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:
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 .c
file:
For example, create a main.c file here
2. Open the main.c file through vivi main.c
(1) Enter vi, at this time in the command mode, command mode, can not perform general input operations.
(2) Enter i
into insert mode
Example:
When in insert mode, input operations are possible:
After typing, press Esc to return to command mode:
(3) Input :
enters the last line mode:
(4) Enter wq, press Enter to save and exit:
3. Compile the C program
After writing a piece of code in main.c, enter in the terminal gcc -o main main.c
to compile the main.c file:
At this time, you can see that an executable program main is generated in the current directory. When we ls -l
use 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:
Equivalent to:
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 .c
program 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.c
non-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.
[Example] Enter vi to write a C language code for initializing an array and save it
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.i
precompiling the main.c file and displaying the result, you can see that the main.i file is generated:
Open this main.i file via vi:
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:
The main.s file is all assembly code:
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
main.o file is a binary machine instruction:
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 printf
the declaration in it. printf
The 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 .o
file.
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:
The main file is the binary executable:
6. General compilation and writing
1. Two steps
First gcc -c main.c
generate .o
the file in one step, then gcc -o main main.o
convert .o
the file into an executable main, and execute it:
2. Take a step
Directly gcc -o main main.c
generate the executable file main in one step, and execute: