Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

The math library functions may not be called, according to GCC document, some inline functions are defined and may be called instead in certain circumstances.

... The GNU C Library provides optimizations for many of the frequently-used math functions. When GNU CC is used and the user activates the optimizer, several new inline functions and macros are defined. These new functions and macros have the same names as the library functions and so are used instead of the latter. In the case of inline functions the compiler will decide whether it is reasonable to use them, and this decision is usually correct.

This means that no calls to the library functions may be necessary, and can increase the speed of generated code significantly. The drawback is that code size will increase, and the increase is not always negligible.

For some reasons gcc optimizes log(const) even with -O0. So there's no log() call in the first case. Check assembly to verify:

gcc sample.c -S

clang, for example doesn't optimize it out on O0. But at O2 gcc optimizes the call in both cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top