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

前端 未结 3 579
广开言路
广开言路 2020-12-17 22:32

I have a sample file here:

#include 
#include 

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

When I compile it

3条回答
  •  盖世英雄少女心
    2020-12-17 22:47

    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.

提交回复
热议问题