user2466999 user2466999 - 3 months ago 12
C Question

Why is -lm not necessary in some cases when compiling and linking C code?

I have a sample file here:

#include <stdio.h>
#include <math.h>

int main(){
printf("%f\n", log(10));
}


When I compile it with
gcc sample.c -o a
it works just fine. I can run it with
./a
and it produces the output
2.302585
like expected.

Yet, when my file looks like this:

#include <stdio.h>
#include <math.h>

int main(){
double a = 10;
printf("%f\n", log(a));
}


it does not compile with
gcc sample.c -o a
. Instead, I have to use
gcc sample.c -o a -lm
so that I can apparently tell it to "link math"...That's where I don't really follow, why wouldn't I have to link math in the first example? And what exactly does it even mean to have to "link math"? It's been a while since I've worked with C compilers, so forgive me if this is poor question.

Answer

Check the disassembly, and you'll likely find that the compiler is optimizing the call to log() out entirely in the first case (so there's nothing to link), but not in the second. In this particular case, glibc defines:

# define M_LN10     2.30258509299404568402

in math.h, for instance, and any standard library function can be implemented as a macro, so it can calculate some of these things without a function call.

Comments