GCC Compilation Process

GCC Compilation Process

GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a “gcc -o hello.exe hello.c” is carried out as follows:

  1. Pre-processing: via the GNU C Preprocessor (cpp.exe), which includes the headers (#include) and expands the macros (#define).
    > cpp hello.c > hello.i

    The resultant intermediate file “hello.i” contains the expanded source code.

  2. Compilation: The compiler compiles the pre-processed source code into assembly code for a specific processor.
    > gcc -S hello.i

    The -S option specifies to produce assembly code, instead of object code. The resultant assembly file is “hello.s“.

  3. Assembly: The assembler (as.exe) converts the assembly code into machine code in the object file “hello.o“.
    > as -o hello.o hello.s
  4. Linker: Finally, the linker (ld.exe) links the object code with the library code to produce an executable file “hello.exe“.
    > ld -o hello.exe hello.o ...libraries...
Verbose Mode (-v)

You can see the detailed compilation process by enabling -v (verbose) option. For example,

> gcc -v hello.c -o hello.exe
Defining Macro (-D)

You can use the -Dname option to define a macro, or -Dname=value to define a macro with a value. The value should be enclosed in double quotes if it contains spaces.



Original Link:



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s