Linux | GCC Compilation Guide

01

Why learn embedded Linux and embedded microcontrollers? Because AI will not be able to replace them in the next decade.

GCC, which stands for GNU Compiler Collection, is a compilation suite that supports various computer architectures such as X86, ARM, and MIPI. The GCC that we use is included by default in Ubuntu.Linux | GCC Compilation GuideGCC options filename-o parameter specifies the name of the generated fileExample: gcc hello.c -o helloIf we do not use the -o parameter to specify the name of the generated file, directly entering gcc hello.c will produce a file named a.out, and executing this a.out is the same as executing hello.02 File command:Function: View file typeFormat: file filenameLinux | GCC Compilation Guide03

Compilation Process

To compile hello.c into hello or a.out, it goes through four steps: preprocessing, compilation, assembly, and linking.

  • hello.i C language code obtained after preprocessing
  • hello.s Assembly language file
  • hello.o Object file

First stage: Preprocessing stage, the compiler will expand header files or macro definitions, or choose conditional compilation. We can use the -E parameter to obtain the preprocessed file.

  • -E: Only preprocess the file, do not compile or link.

Use gcc -E hello.c -o hello.i to obtain the preprocessed file, enter the following command:

gcc -E hello.c -o hello.i 

Second stage: Compilation, compiling the file into assembly code-S parameter compiles hello.i into hello.s file

gcc -S hello.i -o hello.s

Third stage: Assembly, compiling the assembly file into machine code-c parameter can compile hello.s into hello.o file

gcc -c hello.s -o hello.o

Fourth stage: LinkingDirectly compiles the object file into an executable fileLinking can be static linking and dynamic linking, with GCC defaulting to dynamic linkingCharacteristics: The generated program is smaller, but it requires library dependencies,Static linking: Using the -static parameter means static linking, as the program includes the required libraries, resulting in a larger size, the size of the statically linked executable file > dynamically linked executable file.

gcc hello.o -o hello
// Static linking gcc hello.c -o hello1 -static

So I am here

Click the card below to follow me

↓↓↓

Linux | GCC Compilation Guide

If you find it interesting, please click

Save

Like

Looking

+1

❤❤❤Linux | GCC Compilation Guide

Leave a Comment