The process of compiling a C program consists of several stages: preprocessing, compilation, assembling, and linking. Historically, this scheme has allowed for the separation of responsibilities between tools and simplified the maintenance and configuration of the build process.
Problem: If one does not understand how each stage works, they may encounter errors such as "undefined reference", code duplication, and non-obvious bugs due to incorrect use of macros, as well as issues when scaling code across multiple files.
Solution: It is important to understand that each stage performs a specific function: the preprocessor handles directives like #define and #include, the compiler translates the source C code into assembly, the assembler into machine code, and the linker combines all object files and libraries into the final executable file.
Example code:
Snippet of the source code:
#include <stdio.h> #define PI 3.14 int main() { printf("%f\n", PI); return 0; }
Example of calling gcc with explicit stage indications:
gcc -E program.c # Preprocessing gcc -S program.c # Compilation (to assembler) gcc -c program.c # Assembling (to object file) gcc program.o -o prog # Linking
Key features:
What does the directive #include "file.h" do at the compilation stage?
#Include inserts the contents of the file directly at the point of call during preprocessing, before the compilation begins. Including the same file twice can lead to multiple definition errors or issues during linking.
Is defining a function in one file enough for its use in another?
No, a declaration (prototype) in a header file or at least an extern declaration is necessary. Otherwise, errors like "implicit declaration of function" or "undefined reference" may occur during linking.
Can variables declared as static be used from another file?
No. Static limits the visibility of the variable or function to the current file. This means such symbols are not visible to the linker in other object files.
A newbie implements a function with the same name in two source files. A strange error "multiple definition of function" occurs at the linking stage.
Pros:
Cons:
Creating .h files only for declarations and .c files for definitions. Using #ifdef to protect header files. All files are joined through the linker into the final program.
Pros:
Cons: