What has GCC A.C experienced?

Time:2021-3-17

Look at a C language sample source code:

// test.cc
#include <stdio.h>

int main() {
    Printf ("Hello program meow");
    return 0;
}

Compile run

$ gcc test.cc
$ ./a.out
Hello program

As shown in Figure 1,

We usually use GCC to compile programs. This simple command actually goes through a lot of complex processes

  1. Pretreatment
  2. compile
  3. assembly
  4. link

First, take a look with file test.cc File type:

$ file test.cc
test.cc: C source, UTF-8 Unicode text, with CRLF line terminators

Let’s take a look at what each process does?

Pretreatment

Command:

$ gcc -E test.cc -o test.i
perhaps
$ cpp test.cc -o test.i

Let’s look at the file type of test. I

$ file test.i
test.i: C source, UTF-8 Unicode text

It can be seen here that the file type after preprocessing is the same as that before preprocessing, both of which are text files. You can also directly view the content of test. I. there are many codes in it, so it will not be pasted.

In fact, the main operations of preprocessing are as follows:

  • Expand all define macro definitions to replace the text
  • Delete all comments in the program
  • Deal with all conditional compilation, # if, # ifdef, # elif, etc
  • Process all the # include instructions and copy the contents of these header files to the referenced source file
  • Add line number and file name identification to facilitate compiler to generate warning and debugging information
  • Keep all the # pragma compiler instructions because the compiler uses them

compile

Command:

gcc -S test.cc -o test.s

Check the file type again

$ file test.s
test.s: assembler source, ASCII text

As shown in Figure 2, the compilation process is to perform a series of operations on the preprocessed file to generate the corresponding assembly file

  1. Lexical analysis: also known as lexical scanning, through the scanner, using the algorithm of finite state machine, the string in the source code is divided into a series of marks, such as addition, subtraction, multiplication, division, number bracket, etc.
  2. Grammar analysis: use the grammar analyzer to analyze the marks produced by lexical analysis, use the means of context free grammar to analyze, and generate the grammar analysis tree. During this period, if the expression is illegal (brackets do not match, etc.), an error will be reported.
  3. Semantic analysis: syntax analysis checks whether the expression is legal and semantic analysis checks whether the expression is meaningful. For example, if a floating-point integer is assigned to a pointer, the compiler will report an error.
  4. Intermediate language generation: do some syntax tree optimization, such as 6 + 2 = 8.
  5. Object code generation and Optimization: generate intermediate code into object assembly code.

assembly

Command:

$ gcc -c test.s -o test.o
or
$ as test.s -o test.o

View file types:

$ file test.o
testt.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

Using assembler to translate assembly code into machine executable instructions is actually translating assembly instructions and machine instructions one by one according to the comparison table.

link

Why does the assembler not directly generate an executable file, but an object file? Because a file needs to rely on many other libraries, and the symbols of these libraries need to be linked to each other to generate an executable file. It needs to go through the steps of address and space allocation, symbol resolution, relocation and so on. This content is more, which will be introduced in detail in the future We can simply view the libraries that the executable program needs to rely on through LDD. These libraries need to be linked in the process of linking before they can be used.

$ ldd a.out
linux-vdso.so.1 (0x00007ffff5b4a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa1fc660000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa1fce00000)

I’m looking~
More articles, please pay attention to my v x Princess number: program meow adult, welcome to exchange.

Recommended Today

Infinite scroll list

Complexities of an infinite scroller Code example of implementation There are many usage scenarios for infinite scrolling, and some difficulties need to be paid attention to. For example, the links in the footer cannot be accessed because the content keeps pushing the footer away; How do you handle resizing events when the phone changes from […]