Skip to content

Latest commit

 

History

History
176 lines (130 loc) · 6.91 KB

Basic GCC.md

File metadata and controls

176 lines (130 loc) · 6.91 KB

The process of executing a basic C-language program using the GCC (GNU Compiler Collection) involves several steps, from writing the code to running the final executable. Below is a detailed step-by-step explanation of the process:

Step 1: Writing the C Program

The first step is to write the C program using a text editor. For example, let's say you write a simple program like this:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

You save this code in a file with a .c extension, such as hello.c.

Step 2: Preprocessing

The first phase of the compilation process is preprocessing. When you compile a program using GCC, the preprocessor processes the source code to handle directives such as #include, #define, and others.

Command:

gcc -E hello.c -o hello.i

Process:

  • The -E flag tells GCC to stop after preprocessing.
  • This phase replaces #include directives with the contents of the included header files (like stdio.h), expands macros, and handles conditional compilation (#ifdef, #endif).
  • The result is a new file (in this case, hello.i) that contains the expanded source code.

Step 3: Compilation (Conversion to Assembly)

Once preprocessing is done, the next step is compiling the preprocessed code into assembly language, which is specific to the target machine architecture.

Command:

gcc -S hello.i -o hello.s

Process:

  • The -S flag tells GCC to stop after the compilation step.
  • This generates a .s file (hello.s), which contains assembly instructions corresponding to the original C code.
  • At this stage, the human-readable C code is translated into low-level assembly code.

Step 4: Assembly (Conversion to Machine Code)

The next step is to convert the assembly code into machine code, resulting in an object file (.o). This is done by the assembler.

Command:

gcc -c hello.s -o hello.o

Process:

  • The -c flag tells GCC to compile and assemble the code but not link it.
  • This produces an object file (hello.o), which contains the binary (machine-readable) version of the code but is not yet an executable.

Step 5: Linking

In the linking step, the object file(s) are linked with standard libraries (like the C standard library) to create an executable program. The linker resolves function calls (like printf in our program) and integrates them into the final binary.

Command:

gcc hello.o -o hello

Process:

  • This step links the object file hello.o with necessary system libraries and produces an executable file (hello).
  • If your program uses external libraries, they are linked at this stage.

Step 6: Running the Executable

After the linking process, you have an executable file that can be run on your system. You can execute the program like this:

Command:

./hello

Output:

Hello, World!

Summary of Steps

  1. Preprocessing (gcc -E): Expands macros, processes #include directives, and produces a preprocessed output.
  2. Compilation (gcc -S): Converts the preprocessed C code into assembly code.
  3. Assembly (gcc -c): Converts the assembly code into machine code, resulting in an object file.
  4. Linking: Combines the object file with necessary libraries to create an executable.
  5. Execution: Runs the final executable program.

Additional Compilation Options

  • Optimization: You can use optimization flags such as -O1, -O2, or -O3 to optimize the code for performance.

    gcc -O2 hello.c -o hello
  • Debugging: You can generate debugging information using the -g option, which can be useful for debugging with tools like gdb.

    gcc -g hello.c -o hello

Conclusion

Using GCC to compile and run a C program involves several steps, starting from preprocessing the source code and ending with linking the object files to create an executable. These steps—preprocessing, compiling, assembling, and linking—are essential in converting human-readable C code into machine-executable binary files.


If you want to skip the individual steps (preprocessing, compilation, assembly, and linking) and compile your C program directly into an executable, GCC provides a simple way to do this with a single command. By default, when you run GCC without specific flags for intermediate steps, it handles the entire process (preprocessing, compilation, assembly, and linking) internally and outputs the final executable.

Step 1: Write the C Program

As before, let’s assume you have a C program like this saved as hello.c:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Step 2: Compile and Link in One Step

To skip all intermediate steps and directly compile the source code into an executable, you can use the following command:

Command:

gcc hello.c -o hello

Explanation:

  • gcc: This is the GNU C Compiler.
  • hello.c: The source file containing your C code.
  • -o hello: The -o option specifies the name of the output file (in this case, hello). If you omit this option, GCC will create an executable named a.out by default.

This single command performs all the steps for you:

  1. Preprocessing: Handles directives like #include and #define.
  2. Compilation: Converts the C code into assembly.
  3. Assembly: Translates the assembly code into machine code (an object file).
  4. Linking: Links the object file with necessary libraries (like the C Standard Library) and produces the executable.

Step 3: Run the Program

After the compilation, you will have an executable file (hello). You can run the program using the following command:

./hello

Output:

Hello, World!

Additional Options to Control the Compilation

While you can skip the intermediate steps with a simple gcc hello.c -o hello command, there are additional flags that may be useful during the compilation process:

  • Optimization: Use optimization flags for better performance.

    gcc hello.c -O2 -o hello

    The -O2 flag optimizes the code for performance without sacrificing too much compilation time.

  • Debugging: Add debugging information with the -g option.

    gcc hello.c -g -o hello

    The -g flag includes debugging information, which can be used with tools like gdb for troubleshooting your program.

  • Warnings: Enable warnings with the -Wall option, which helps catch potential issues in your code.

    gcc hello.c -Wall -o hello

    The -Wall flag turns on most compiler warnings to alert you to potential problems in your code.

Summary of Skipping the Steps:

To directly compile and link a C program without manually running each step, you can use the basic gcc hello.c -o hello command. This compiles the code and produces the executable in one step, making the process quicker and simpler for small or typical programs.