I'm not interested in the source code, I want to know how the C compiler (GCC) actually finds the functions. As in, when the preprocessor sees that I've included stdio.h
, where does it look to find the files that define the function bodies?
Edit
I should probably also say I'm using Ubuntu 12.04, but if there's a general answer, that would work too.
All C standard library functions are declared by using many header files. These library functions are created at the time of designing the compilers. We include the header files in our C program by using #include<filename. h>. Whenever the program is run and executed, the related files are included in the C program.
The interface of C standard library is defined by the following collection of headers. <assert.h> Conditionally compiled macro that compares its argument to zero. <complex.h> (C99)
Functions are available in a number of libraries. These are accessed by including header files in your code; the header file is a pointer/reference to the library. Library functions include standard input/output (stdio. h), string manipulation (string.
The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With