Search code examples
clinkerpic32

When including headers in a C program, how does the linker/compiler find the corresponding code?


I understand how linking and compiling works when creating .c and .h files and adding them to my project.

But what happens when I add headers like stdio.h to my project? I understand that the linker searches for the .h file in some standard directories, then pastes it in, but the header contains only function prototypes and no code. Where does the compiler or linker find the code for these functions, and how does it get added to my source files?

The reason I ask is because I'm writing a bootloader for a microcontroller and I would like to carefully look through all the C code that's actually getting sent to the compiler. I'm using the non-optimized free version of the XC32 compiler for the PIC32, so I don't trust that it's only including what I'm actually using.


Solution

  • You need to distinguish between headers and libraries.

    Headers declare facilities that are available to programs. When you include a header such as <stdio.h> (NB: this is not — repeat not! — a library), you make available to the compiler the information necessary to use the facilities from the standard I/O library. In general, C headers do not define the actual code that implements the facilities. C++ has 'header only' libraries (some parts of Boost are prime examples of 'header only' libraries).

    Libraries provide the implementation of the facilities. The <stdio.h> header declares a function fopen(); there is a library somewhere that defines that function.

    Some headers (actually, typically, it is a lot of headers) are privileged and the facilities that they declare are included in the standard library that the C compiler links your programs with. You don't have to do anything special to get the functions linked into your program. Other headers are from libraries that the C compiler does not know about a priori, and for those, you have to tell it where to find the library (e.g. with -L /opt/sometool/lib as a compiler option) and the library name (e.g. with -lsometool, which might link with /opt/sometool/lib/libsometool.so or /opt/sometool/lib/libsometool.a). Note that the headers for SomeTool are probably in /opt/sometool/include, and you'd need to add an option -I/opt/sometool/include to find the sometool.h header.

    The linker doesn't reference headers; the compiler proper doesn't reference libraries. The compiler control program does handle the mixture (it typically runs multiple phases of the compilation process as separate programs — the compiler is separate from the linker). The headers do not contain information about where the library is installed.