Where are the functions in the C standard library defined? Where are the functions in the C standard library defined? c c

Where are the functions in the C standard library defined?


gcc comes with (binary) object files (not C source files) which contain implementations of all the standard C functions. When you use gcc to link object files into an executable file, the linker automatically includes the object files which implement the standard library functions. According to this thread, that standard object file will probably be called libc.a or libc.so.

Say you include a call to printf in your program. When the linker tries to resolve where that call should go, it will find the definition of printf in libc.a, and make your function call point there.

Look at http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html and note the -nostdlib and -nodefaultlibs options. You can use these options to tell gcc's linker not to include the standard library object files by default.


gcc obtains the function definitions from the C library. You could determine the path that gcc would look into, by default, for it by saying:

ld --verbose | grep SEARCH_DIR

This leads to /usr/lib on my system.

Lets try to find if the library contains the symbol for a standard function, say scanf:

nm -A /usr/lib/libc.so | grep scanf

The results include:

/lib/libc.so:0000000000042a90 T scanf

Consider a small example:

#include <stdio.h>int main() {  printf("Hello World!\n");  return 0;}

Lets call it i.c:

$ gcc i.c              # Compile$ ldd ./a.out          # Try to find dependencies./a.out:        -lc.12 => /usr/lib/libc.so.12

The last command essentially implies that the binary depends upon /usr/lib/libc.so.12 and that you'd find the definitions of the functions used in the code therein.


Your question has to do with where GCC searches for header files. It searches the standard include directories. You may find this thread to be useful:

With various options (such as -I and -I- and -isystem) you can specify lots of different inclusion features. Basically, directories specified by -I will be searched before those specified by -isystem, which will in turn be searched before those in the "standard system include directories" (at least, according to my tests). The difference is that -I can be used for any #include directive, but -isystem will only be used for #include <...> That said, though, it is recommended to only use -I for #include "..." directives because of the search order. Using -I- really gives you a lot of control because any -I used before -I- will be searched for only for #include "..." whilst any -I used after -I- will be searched for any #include directive. In addition, using -I- means that the current directory will not be searched for included files unless you also specify -I. (search the current directory).

If you want to get a listing of what search directories are supported by default, try running this command: cpp -v < /dev/null This runs the GNU C Preprocessor with no input; in the process it will print (given the -v flag) the inclusion directory search paths. You should see phrases like "#include <...> search starts here:" followed by a list of directories. Those are your standard inclusion search paths, in the order that they're searched.