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.
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.
Your libc
(or libstdc++
for C++) might be located in either /usr/lib
or /usr/lib64
on Linux. These are shared libraries and you can modify the LD_LIBRARY_PATH
variable to specify what directories they're searched in. A practical example would be you install a local copy of gcc and chances are it will have an updated version of the standard library as opposed to your system, so you would want your local gcc to start up with that instead, i.e export LD_LIBRARY_PATH=/home/user/local-install/gcc/lib64
It looks into the library paths set by the environment variable.
Read more: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html
来源:https://stackoverflow.com/questions/22240946/where-are-the-functions-in-the-c-standard-library-defined