Compilation and debugging of multi-file C program on Linux system

1. First create a header file add.h

Create an add.h through vi, and write this file to store the declaration of the summation function add():

insert image description here

2. Create an add.c file

Create an add.c file through vi and write it to store the implementation of the summation function add():

insert image description here

3. Create a max.h header file

Create a max.h through vi, and write this file to store the declaration of the maximum value function max():

insert image description here

Fourth, create a max.c file

Create a max.c through vi, and write this file to store the implementation of the maximum value function max():

insert image description here

5. Create the main function main.c

Create a main.c through vi, and write this file to realize the functions of the summation function add() and the maximum value function max():

insert image description here

6. Compile

(1) One-step compilation

By gcc -o main main.c add.c max.ccompiling, the executable file main is generated:

insert image description here

(2) Two-step compilation

① First convert the .c file to a binary instruction .o file

Convert add.c file to binary instruction add.o file; convert max.c file to binary instruction max.o file; convert main.c file to binary instruction main.o file:

insert image description here

② Link to generate executable program main

insert image description here

Seven, makefile and make

The content in the makefile describes how to compile the entire project. The role of the makefile is to manage the entire project. The real makefile is generated by the tool. There is no need to write header files in the makefile. Each project is equipped with a makefile, and only the make command is needed to complete the compilation work during compilation.

The make command will read the content in the makefile and compile the code according to the makefile.

(1) Writing makefile

By vi makefilecreating a makefile:

insert image description here
Write makefile in vi:

Suppose now to generate the final executable main:

The first step: specify the final generated executable program main

Format:

all:main

Step 2: Write the dependent files of the executable program main and the compilation rules of the dependent files

Format:

main:main.o add.o max.o
	gcc -o main.o add.o max.o

When compiling main.o add.o max.othese three files, the tab key (indentation) must be entered in front of gcc.

Step 3: Write the dependent files of the main.o, add.o, max.o files of the executable file main and the compilation rules of the dependent files

Format:

main.o:main.c
	gcc -c main.c
add.o:add.c
	gcc -c add.c
max.o:max.c
	gcc -c max.c

The above three steps are summarized as all to designate the final generated executable program as the main program, and then our program will find the main program and its dependent files main.o, add.o, max.o, and then compile The rule gcc -o main.o add.o max.ogenerates the executable program main. If the dependent files main.o, add.o, max.o of the main program are not found, then the dependent files main.c, add.c, max.o of main.o, add.o, max.o will be found. c, after finding it, gcc -c main.cconvert the main.c, add.c, and max.c files into main.o, add.o, and max.o files through the compilation rules, , respectively, and then generate the executable program main through the compilation gcc -c add.crules .gcc -c max.cgcc -o main.o add.o max.o

Step 4: Clean up the project, delete the code that is not written by yourself in the whole process, that is, some files generated in the middle.

Format:

clean: 
	rm -rf *.o main

-r is to delete some directory files that may be generated, and -f is to ignore the fact that main is not generated when deleting. In the above example, all .o files and main are some files generated during this process, so delete them Lose.

Specifically as shown in the figure below:

insert image description here

After saving and exiting, you can use the make command to compile the code.

The installation command can also be written in the makefile install, but the installation command needs to be implemented with the make command in the administrator mode.

(2) The make command compiles the code and executes the program main after the compilation is completed

insert image description here

After the make command is executed, you can make cleandelete the .o file and the main program by executing the corresponding rules:

insert image description here
① At this time, execute the make command twice:

insert image description here

This result occurs because we have not modified the code, so there is no need to compile it, and the entire code is up to date.

② If you modify the code in add.c, add a space in the code (add a space before the semicolon) and save it, the logic of the code does not change:

insert image description here

At this point, compile the code with the make command:

insert image description here
It can be found that add.c is recompiled, and then reconnected to generate the final main program.

Eight, run

Run executable by path:
insert image description here

Nine, debugging

The program can be debugged only after the executable program is generated, and the debug program is mainly to solve logical errors and software permissions. That is, the compilation can only be debugged successfully. If the compilation is not successful, there is a syntax problem. Debug the program when the running result after the compilation is successful is different from the expected result.

The object of debugging: the running program (process). It is not debugging main.c nor debugging main, but debugging the running main.

Tracking program: gdb is a tool for tracking programs. It can only track programs with debugging information, that is to say, only the debug version can be tracked but not the release version.

1.Debug version and Release version

(1) Debug version

The Debug version is an adjustable version, and the generated executable file contains the information needed for debugging. As developers, the most commonly used is the debug version of the executable file.

(2) Release version

The Release version is a release version, which is a version provided to users. The default version generated by gcc is the Release version, which does not contain debugging information.

First compile and link the source code to generate the executable file of the Debug version, and then enter the debug mode through the 'gdb Debug version executable file name'.

(3) Generate debugging information in the Release version

Because debugging information is added to the intermediate file (.o) during compilation, it must be controlled during compilation to generate an intermediate file containing debugging information.

Generate debug debug info: gcc -c main.c -g:

insert image description here

It can be seen that the size of the executable program main is 16816 when no debugging information is generated, and 19944 after generating the debugging information, indicating that it is a debugging version at this time.

Generate an intermediate file containing debug information: gcc -o hello hello.oorgcc -o hello hello.c -g

(4) Include the debug version in the makefile

insert image description here

Line 8 gcc -o main main.o add.o max.ois a linking process, not a compilation process, and the debugging information is generated during the compilation process, so it gcc -o main main.o add.o max.ocannot be written later $(GDB).

At this point, compiling with the make command is the debug version:

insert image description here
GDB=-gIf you want to use the Release version next time, then change the makefile GDB= , and then switch from the debug version to the Release version.

Guess you like

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