Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 978
user2466999 Avatar asked Oct 21 '13 05:10

user2466999


2 Answers

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.

like image 175
Crowman Avatar answered Nov 10 '22 18:11

Crowman


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.

like image 41
Yu Hao Avatar answered Nov 10 '22 19:11

Yu Hao