<>1. gcc Installation of

<>2. Step by step compilation connection

C The compiling process of language :
One C Language program , Pretreatment is required , compile , assembly , link , To get the executable program

gcc Provide distributed compilation links , They are :

* Pretreatment : gcc -E main.c -o main.i
* compile : gcc -S main.i -o main.s
* assembly : gcc -c main.s -o main.o
* link : gcc main.o -o main
* You can also perform direct preprocessing step by step to compile the assembly link successfully , Execute the following statement : gcc -o main main.c gcc main.c
Last generated main Is an executable program , If there are no mistakes , Run this file directly
This process is demonstrated below :

*
I wrote one main.c

*
Pretreatment

*
compile , assembly , link

*
function main program

*
One step preprocessing compilation assembly link

<>3. Compile link process

<>3.1 Precompile phase

* Delete all “#define”, And expand all macro definitions , Expand macro definition , Replace the macro value in the program ;
* Handle all conditional precompile instructions ,“#if”,“#ifdef”,“#endif” etc. ;
* handle “#include” Precompile instruction , Insert the included library file into the file ;
* Delete all program comments ;
* Add line number and file name identification , It is convenient for the compiler to generate symbol information for debugging and display line number when compiling errors and warnings ;
* Keep all #pragma Compiler Directives , Because the compiler needs to use them .

<>3.2 Compilation phase

lexical analysis , Grammatical analysis , semantic analysis , Code optimization , Summary symbol .

<>3.3 Assembly phase

Translate assembly instructions into binary format , Generate each section, Generate symbol table

<>3.4 Linking process

* Merge all section, adjustment section The initial displacement and segment size of , Merge symbol table , Analyze the symbols , Assigning virtual addresses to symbols
* Symbol relocation


The above compilation process , It's not particularly troublesome when it comes to a single file , But if there are more than one C When language files need to be compiled , It's very troublesome , We found that windows In the system , implement C Language program , We don't see individual processes , Just click the execute button to execute , actually windows Some compilers of , Automatically write us a key execution file , When we click the execute button , Call this executable file , Automatic implementation of compilation and other processes , stay Linux in , In fact, we can also do this operation , We can create a file according to the following procedure , By adding some statements to the file , To achieve the compilation of multiple files and other processes .
<>4. makefile and make

Management Engineering , Realize automatic compilation , as follows :

*
Write one add.h

*
Write one add.c

*
Write one max.h

*
Write one max.c

*
Write one main.c

*
Compile in a traditional way

*
use makefile To achieve

What's in it make not installed , You will be prompted to follow what command to install , Follow the instructions to install

<>5. gdb debugging

<>5.1 Debug and Release

* Debug
Debug Versions are often referred to as adjustable versions , The generated executable file contains the information needed for debugging , As a developer , The most common is debug Version of the executable
Because debugging information is added to the intermediate file at compile time .o In , Therefore, it must be controlled to generate intermediate files containing debugging information at compile time
Execute the following statement , You can get the intermediate file : gcc -o hello hello.c -g
* Release
Release version , Version provided to users , adopt gcc The default generation is Release edition
about C Language debugging process , First, by compiling the source code , Connection generation Debug Version of the executable , And then through
gdb Debug The executable name of the version
Enter debug mode

<>5.2 Single process , Single thread basic debugging command

* First, write one test.c file , Let's perform some common tasks gdb debug information , The code is as follows :

From the above we can see some common instructions , such as : b Line number // Add breakpoint to line number b Function name // Add a breakpoint to the first line of the function info break
// Displays information about breakpoints l // Display source code Click to generate a piece of code , Will continue to show the reality above r // Run the program n // Step program s
// Enter the function to be called to execute finish // Jump out function q // Exit debugging p val
// Print the value of the variable , Pay attention to the inside val It could be an address , It can also be an expression , It can also be all elements of an array c // Go ahead

Technology