Compilation Process

Compilation Process

Preprocessing:

  • The preprocessor handles directives like #include, #define, and conditional compilation.
  • It expands macros, includes the contents of header files, and processes conditional compilation instructions.
  • The output is a preprocessed source file with the .i extension (in C) or .ii extension (in C++).

gcc -E example.c -o example.i        



Compilation:

  • The compiler translates the preprocessed source code into assembly code for the target architecture.
  • Syntax and semantic analysis are performed during this stage.
  • The output is an assembly file with the .s extension.

gcc -E example.c -o example.i        

Assembly:

  • The assembler converts the assembly code into machine code, generating an object file.
  • The output is an object file with the .o (or .obj on Windows) extension.

gcc -S example.c -o example.s        

Relocatable object files

contain processor architecture-specific machine code with no absolute addresses, allowing for flexible placement in memory during the linking process. They enable the generation of executable files by allowing the linker to adjust addresses and resolve symbols according to the final memory layout of the program.


Linking:

  • The linker combines object files and libraries into a single executable.
  • It resolves symbol references, assigning addresses to various code and data sections.
  • The output is an executable file.

gcc example.c -o example        

Additional Details

Intermediate Files: During the compilation process, various intermediate files are generated:

  • Preprocessed source file (.i or .ii).
  • Assembly file (.s).
  • Object file (.o or .obj).



 Linking Libraries: The linker can link against standard libraries (like the C standard library) and custom libraries.

  • Static libraries have the .a (Unix/Linux) or .lib (Windows) extension.
  • Dynamic libraries have the .so (Unix/Linux) or .dll (Windows) extension.



Compiler Flags

Compiler Flags: Various flags can be used to control the compilation process:

  • -I to specify include directories for header files.
  • -L to specify library directories.
  • -D to define macros.
  • -O to control optimization levels.
  • -g to include debugging information.
  • -Wall to enable all compiler's warning messages.




 table of key gcc flags :

Article content
table of key gcc flags


 

 

 

 

 

To view or add a comment, sign in

More articles by Abdallah Ghazy

  • Parts of the X-ray imaging system I

    An X-ray machine has the following main parts: X-ray Generator An X-ray generator is a component within the X-ray…

  • MCU Clocking and Clock Management Unit (CMU)

    Overview: MCU clocks are crucial timing signals within a microcontroller, ensuring that all components operate in sync…

  • What Happen When We Exceed Valid Range of Built-in Data Types in C?

    Signed Integers (signed int) 🟢 Definition: Signed integers can hold both positive and negative values. The range of a…

  • Startup Code in Embedded C

    In embedded systems, the startup code is responsible for initializing the hardware and software environment for the…

  • Bare metal Embedded SW

    are metal embedded software referring to programming in embedded systems without an operating system (OS) or middleware…

Explore content categories