lm flag is needed to link the math library?
This question already has an answer here:
Because of ridiculous historical practice that nobody is willing to fix. Consolidating all of the functions required by C and POSIX into a single library file would not only avoid this question getting asked over and over, but would also save a significant amount of time and memory when dynamic linking, since each .so
file linked requires the filesystem operations to locate and find it, and a few pages for its static variables, relocations, etc.
An implementation where all functions are in one library and the -lm
, -lpthread
, -lrt
, etc. options are all no-ops (or link to empty .a
files) is perfectly POSIX conformant and certainly preferable.
Note: I'm talking about POSIX because C itself does not specify anything about how the compiler is invoked. Thus you can just treat gcc -std=c99 -lm
as the implementation-specific way the compiler must be invoked for conformant behavior.
Because time()
and some other functions are builtin
defined in the C library ( libc
) itself and GCC always links to libc unless you use the -ffreestanding
compile option. However math functions live in libm
which is not implicitly linked by gcc.
上一篇: 调试GCC编译时间
下一篇: lm标志是需要链接数学库吗?