If I include <stdlib.h> or <stdio.h> in a C program, I don’t have to link these when compiling, but I do have to link to <math.h>, using -lm with GCC, for example:
gcc test.c -o test -lm
What is the reason for this? Why do I have to explicitly link the math library, but not the other libraries?
The functions in
stdlib.handstdio.hhave implementations inlibc.so(orlibc.afor static linking), which is linked into your executable by default (as if-lcwere specified). GCC can be instructed to avoid this automatic link with the -nostdlib or -nodefaultlibs options.The math functions in
math.hhave implementations inlibm.so(orlibm.afor static linking), andlibmis not linked in by default. There are historical reasons for thislibm/libcsplit, none of them very convincing.Interestingly, the C++ runtime
libstdc++requireslibm, so if you compile a C++ program with GCC (g++), you will automatically getlibmlinked in.