简体   繁体   中英

C Preprocessor directives and Linking

The #include directive will result in placing the content of the header file in the source code before compilation : for example if I included stdio.h , the preprocessor will work on placing all the content of stdio.h in the source code then compile isn't it ?

So let's pretend that I'm just using the printf() function in my code. So there must be something that will happen after the compilation and between the linking that will delete all function implementations that were included from the header file and only insert the printf() function implementation in the Import table of the executable , knowing that all other functions were compiled with the source. Can you explain that to me please ?

The printf function is not actually in the header file, it's in a library that is automatically linked to the executable by the linker.

Header files should contain only function prototypes , ie the declaration of the functions and not the definitions. And as there's no function definitions in the header file, no code will actually be generated for them, and the compiler will make sure that only the functions you actually call will get an entry in the generated object file so the linker knows about it.

A function prototype declared in a C header file acts as a compile-time constraint. It identifies the correct number of parameters and correct types of those parameters. The verification of such constraints happens during the compilation phase (ie *.c -> *.o ).

The (static) linking phase eliminates unused binary objects from programming libraries on a per-object-file basis. A programming library is an archive of binary object files (*.o), each containing the implementation of a collection of functions, constants, etc. As for your question, the binary object file containing the implementation of printf (and everything else in the same object file) will be linked into your program. Other unused object files will be eliminated as a link-time optimization.

The header file only contains a declaration , eg of the form

size_t printf(char const *format, ...);

This tells the compiler that if it encounters the word printf and it is used as a function, then it can generate a function call.

The call instruction contains a placeholder for the actual address of the function, which is then inserted by the linker when building the final executable.

The linker generally just keeps a running list of yet unresolved symbols , and adds to this list when it encounters one of these placeholders, and fills in the actual address when it finds a definition for the symbol. In the case of printf , that definition is found in the C standard library, so the linker ensures that the library is loaded at runtime and the call instructions point at the right address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM