[Linux] Use of GCC compiler

Table of contents

Foreword:

 1. GCC compilation process

1. Pretreatment:

2. compile

3. Compilation

4. link

2. Making and using dynamic and static libraries

1. Static library

2. Dynamic library

 Three, useful options

 1.gcc -E main.c

 2.gcc -E -dM main.c > 1.txt

  3.gcc -Wp,-MD,abc.dep -c -o main.o main.c

  4.echo 'main(){}'| gcc -E -v -


Foreword:

2-1. GCC compilation process_哔哩哔哩_bilibili

2-2. GCC common options_哔哩哔哩_bilibili

(1) The gcc compilation tool chain on the PC can compile programs that can run on the X86 platform. To compile programs that can run on the ARM platform, you must use the cross-compilation tool xxx-gcc.

(2) A C/C++ file needs to go through four steps of preprocessing (generating .i file) , compiling (generating .s) , assembling (generating .o file) and linking (linking to the application program) before it can become an executable file. as follows:

(3) We can control the output of different files through different gcc options. The commonly used options are as follows:

 1. GCC compilation process

1. Pretreatment:

Common preprocessing commands in C/C++ source files include include commands (#inlcude), macro definition commands (#define) and conditional compilation commands (#if, #ifdef).

Preprocessing is to insert the file to be included into the original file, expand the macro definition, select the code to be used according to the conditional compilation command, and finally output these things to an .i file for further processing

2. compile

Compilation is to "translate" the C/C++ code (the above .i file) into assembly code.

3. Compilation

 Assembly is to translate the assembly code output in the second step into machine code conforming to a certain format, which is generally expressed as an ELF object file (OBJ file) on a Linux system.

4. link

Linking is to link the OBJ file generated in the previous step with the OBJ file and library file of the system library, and finally generate an executable file that can run on a specific platform. 

2. Making and using dynamic and static libraries

Pay special attention to the path of the used library.

1. Static library

The file compiled by using the static library is relatively large, and all the data of the entire function library is integrated into the target code. Its advantage is that the compiled execution program does not need external function library support.

gcc -c -o main.o main.c
gcc -c -o sub.o sub.c
//Create a static library from the .o file
ar crs libsub.a sub.o 
//(If .a is not in the current directory, you need to specify its absolute or relative path)
gcc -o test main.o libsub.a 

Note: Use the ar command to generate a static library, you must use the .o file to generate it (other files cannot be used). And the generated .a file must start with lib (or the library cannot be found).

2. Dynamic library

The dynamic function library is not compiled into the target code when compiling, and the corresponding function in the function library is called when the program executes to the relevant function.

Therefore, compared with the static library, the dynamic library saves system space. When encountering a bug in the library, just replace the original library with a new one.

gcc -c -o main.o main.c
gcc -c -o sub.o sub.c
gcc -shared -o libsub.so sub.o
gcc -o test2 main.o -L   /directory/  -lsub 
//(Take the current directory as an example)
gcc -o test2 main.o -L ./ -lsub

 Note: The generated dynamic library file is in the current folder, and its path needs to be added to lib or the path specified when compiling.

If there is no lib, you also need to run the following command ( specify the path ) at runtime:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH: ./
./test2

Run tests ( specify path ): 

 Three, useful options

 1.gcc -E main.c

View the preprocessing results, such as which header file is  the specific result:

 2.gcc -E -dM main.c > 1.txt

 Expand all the macros and store them in 1.txt, a specific example (open 1.txt): 

  3.gcc -Wp,-MD,abc.dep -c -o main.o main.c

Generate the dependent file abc.dep, which will be used later in the Makefile, specific example (open abc.dep):

  4.echo 'main(){}'| gcc -E -v -

List its header file directory and library directory (LIBRARY_PATH) when compiling 'main(){}', specific example:

Guess you like

Origin blog.csdn.net/weixin_42373086/article/details/129887540