Take the 2-minute tour ×

I was learning the C language and was peeking through some header files in the Linux directory /usr/include such as stdio.h, stdlib.h etc. What really was bothering me was that I see all functions define with an extern variable, which means they are only being declared without any definition such as:

extern FILE *fopen (__const char *__restrict __filename,
                    __const char *__restrict __modes) __wur;

The same goes for every other function in every other header file. My question is, if they are only being declared where are their implementations? They have to be implemented somewhere right?

share|improve this question
Headers, particularly system headers, are not suitable to learn basic C. Learn the language first without worrying about language abuse like what is employed in there. –  pmg May 10 '11 at 19:42
The implementations are in the library, as any basic text on the C language will tell you. ... "extern variable" -- you mean "extern keyword". –  Jim Balter May 10 '11 at 19:52
Note that the extern keyword is purely extraneous in function declarations. All it does is waste disk space and parser time when the compiler parses it. Of course the comments and argument names do this too.. –  R.. May 10 '11 at 23:26
add comment

3 Answers

Those are called function prototypes. They tell the compiler that the function exists, but not where (yet). The compiler uses this to make sure that you're calling the function correctly, but that's it.

Once the compiler is done, the linker gets called. This is where the magic happens. The linker determines which library has the actual implementation of the function. In this case it's probably going to be in the standard library (called libc on some systems), which is automatically pulled in. The linker does its thing, and your calls to that function are then handled by the library.

If the prototype exists, but the implementation can't be found, you get a linker error (something along the lines of "undefined symbol"). If the prototype is missing, the code will compile but you'll probably get a warning about it (thanks Jim Balter for the info about this).

share|improve this answer
"Likewise if the function is undefined, but the implementation exists, you'll get a compiler error" -- Wrong. The implementation is the definition. If you means "undeclared", no declaration of functions is necessary. "undefined reference to..." -- There's no such error message for functions. You will get a (different) error if the usage doesn't agree with the implicit declaration of the function. –  Jim Balter May 10 '11 at 19:59
P.S. "If the function is defined, but the implementation can't be found" -- That should read "If the function is referenced, ..." –  Jim Balter May 10 '11 at 20:01
Updated to reflect correct terminology. –  Chris May 10 '11 at 20:09
Better but, again, there's no such error for missing prototypes -- prototypes aren't necessary; please test your claims. And both claims are still wrong: merely having a prototype doesn't produce a linker message; you have to actually reference the function. –  Jim Balter May 10 '11 at 21:06
@Jim Balter: Hmm, I thought I did. I went back and made a small test program and by god you're right. I got an error both times, I didn't realize until just now that it was the linker giving the error in both instances. My bad. –  Chris May 10 '11 at 21:08
show 1 more comment

The .h files contain the declarations, the definitions are held somewhere in some object file. More info can be found here.

share|improve this answer
add comment

Header files only define the interface to the standard library functions, not the implementation; they (as a rule) don't contain any executable code.

It depends on the compiler and platform, but typically the standard library functions have already been compiled and collected into binary files that your code is linked against to produce an executable.

If you're on a Linux system using gcc, you can find these library files under /usr/lib. libc.a typically contains the bulk of the C standard library functions (stdio, stdlib, string, etc.). Math functions are stored separately in libm.a. Under normal circumstances, gcc links against /usr/lib/libc.a automatically, so you don't have to worry about it. If you need to use math library functions, you need to add -lm to the command line to link against the math library.

Note that most implementations do not ship the source code for the library functions themselves; all you get is the precompiled binary files.

share|improve this answer
add comment

Your Answer


