Compilation and debugging of C program on Linux system

1. Environment configuration

In the Linux operating system (ubuntu), open the terminal and log in as administrator root mode
1. Update: enter the command apt update
2. Download vim: enter the command apt install vim -y
3. Download gcc: enter the command apt install gcc -y
4. Download g++: enter the command apt install g++ -y
5. Download make: Enter the command apt install make -y
to install successfully:
insert image description here

Second, create a c program

[Example 1] Output Hello word!

1. First enter the directory where you want to create a file, and then create a .cfile:

For example, create a main.c file here

insert image description here
2. Open the main.c file through vivi main.c

insert image description here
(1) Enter vi, at this time in the command mode, command mode, can not perform general input operations.
insert image description here

(2) Enter iinto insert mode

Example:

insert image description here

When in insert mode, input operations are possible:

insert image description here

After typing, press Esc to return to command mode:

insert image description here

(3) Input enters the last line mode:

insert image description here

(4) Enter wq, press Enter to save and exit:

insert image description here
insert image description here

3. Compile the C program

After writing a piece of code in main.c, enter in the terminal gcc -o main main.cto compile the main.c file:

insert image description here
At this time, you can see that an executable program main is generated in the current directory. When we ls -luse it to view it, we find that the generated file has executable permissions, which means it can run.

Fourth, execute the C program

Enter the path of the executable program to execute the C program to be executed:
insert image description here

Equivalent to:
insert image description here

5. Compilation and linking process of c program

The suffix of the source program of c language must be .c, and the source file is stored on the hard disk for persistent storage. The C language .cprogram cannot be executed directly. It must be compiled and linked to generate a binary executable program that can be executed on the computer, so that the binary executable program can be run. It can be converted into an executable binary program main by converting the gcc -o main main.cnon-executable program..c

The process of program compilation is divided into 4 steps: precompilation, compilation, assembly and linking. Through these four steps, the high-level language is translated into machine instructions (binary), and what can be directly executed on the computer is the final binary machine instruction.
insert image description here

[Example] Enter vi to write a C language code for initializing an array and save it

insert image description here

1. Precompilation stage

(1) Delete all "#define" and expand all macro definitions;
(2) Process all conditional precompiled directives, "#if", "#ifdef", "#endif" and so on; (
3) Process " #include" precompiled directive, insert the included file into the position of the precompiled directive;
(4) delete all comments;
(5) add line number and file name identification, so that the compiler can generate symbolic information for debugging And display line numbers when compilation errors and warnings are generated during compilation;
(6) Keep all #pragma compiler directives, because the compiler needs to use them.

Example:

After deleting the main file, precompile the current main.c file. By gcc -E main.c -o main.iprecompiling the main.c file and displaying the result, you can see that the main.i file is generated:

insert image description here

Open this main.i file via vi:

insert image description here
insert image description here

You can see that the include file is fully expanded, the macro is also expanded, and the comment is deleted

2. Compilation phase

Lexical analysis, syntax analysis, semantic analysis, code optimization, summary symbols.

Example:

Pass gcc -S main.i -o main.s, convert the pre-compiled file main.i to the compiled file main.s, and open the main.s file with vi:

insert image description here

The main.s file is all assembly code:

insert image description here

3. Compilation stage

Translate assembly instructions into binary format, generate each section, and generate a symbol table.

Example:

Pass gcc -c main.s -o main.o, convert the file main.s generated in the compilation phase into the compiled binary machine instruction main.o, and open the main.o file with vi: The
insert image description here
main.o file is a binary machine instruction:
insert image description here
the ELF in the above figure is the file's Format.

Although main.o is a binary instruction file, it cannot be executed, because the current main.o translates the main.c file we wrote into a binary instruction. main.o is only a part of our entire program at this time, because The code we wrote uses a lot of other people's code. For example priintf, we didn't implement it ourselves. We #include<stdio.h>just put printfthe declaration in it. printfThe implementation of it is in the C library function, so we need to link it before it can run. stand up. Therefore, in addition to the code already written, the final executable program also links some basic code that will be used. What is placed in the library is actually the compiled .ofile.

4. Linking stage

(1) Merge each section, adjust the starting displacement and segment size of the section, merge the symbol table, perform symbol analysis, and assign a virtual address to the symbol. (
2) Symbol relocation.

Example:

Pass gcc -o main main.o, convert the file main.o generated in the assembly stage into a binary executable program, and open the main file with vi:

insert image description here

The main file is the binary executable:

insert image description here

6. General compilation and writing

1. Two steps

First gcc -c main.cgenerate .othe file in one step, then gcc -o main main.oconvert .othe file into an executable main, and execute it:

insert image description here

2. Take a step

Directly gcc -o main main.cgenerate the executable file main in one step, and execute:

insert image description here

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132216304