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?
There's a thorough discussion of linking to external libraries in An Introduction to GCC - Linking with external libraries. If a library is a member of the standard libraries (like stdio), then you don't need to specify to the compiler (really the linker) to link them.
EDIT: After reading some of the other answers and comments, I think the libc.a reference and the libm reference that it links to both have a lot to say about why the two are separate.
I think it's kind of arbitrary. You have to draw a line somewhere (which libraries are default and which need to be specified).
It gives you the opportunity to replace it with a different one that has the same functions, but I don't think it's very common to do so.
EDIT: (from my own comments): I think gcc does this to maintain backwards compatibility with the original cc. My guess for why cc does this is because of build time -- cc was written for machines with far less power than we have now. A lot of programs have no floating-point math and they probably took every library that wasn't commonly used out of the default. I'm guessing that the build time of the UNIX OS and the tools that go along with it were the driving force.
An explanation is given here:
[Edit]
I'm not sure I agree with this, though. If you have a library which provides, say,
sqrt()
, and you pass it before the standard library, a Unix linker will take your version, right?stdlib.h
,stdio.h
are the header files. You include them for your convenience. They only forecast what symbols will become available if you link in the proper library. The implementations are in the library files, that's where the functions really live.Including
math.h
is only the first step to gaining access to all the math functions.Also, you don't have to link against
libm
if you don't use it's functions, even if you do a#include <math.h>
which is only an informational step for you, for the compiler about the symbols.stdlib.h
,stdio.h
refer to functions available inlibc
, which happens to be always linked in so that the user doesn't have to do it himself.As ephemient said, the C library libc is linked by default and this library contains the implementations of stdlib.h, stdio.h and several other standard header files. Just to add to it, according to "An Introduction to GCC" the linker command for a basic "Hello World" program in C is as below:
Notice the option -lc in the third line that links the C library.
stdio is part of the standard C library which, by default, gcc will link against.
The math function implementations are in a separate libm file that is not linked to by default so you have to specify it -lm. By the way, there is no relation between those header files and library files.