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

前端 未结 3 577
广开言路
广开言路 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 23:04

    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.

提交回复
热议问题