当前位置:网站首页>Development tools -- gcc compiler usage

Development tools -- gcc compiler usage

2022-07-05 07:52:00 Soy sauce;

1.GCC compiler

GCC(GNU Compiler Collection, namely GNU Compiler package ), It's a set of GNU Developed programming Language compiler . It's a set of GPL And LGPL Free software released by license , It's also GNU The key part of the plan branch , It is also a kind of freedom Unix And Apple Computer Mac OS X Standard compiler for operating systems .GCC( Especially its Medium C Language compiler ) It's also often considered the de facto standard for cross platform compilers .
Linux Under the system GCC The compiler is actually GNU Compile a software in the tool chain , It can be used to call Other different tools such as pretreatment 、 compile 、 Compilation and linking such work .GCC Not only powerful , sex Can be superior , Its execution efficiency is higher than that of general compilers 20%~30%, And because it is GNU One of the project , It's open source software .
Often used in .c Generate exe Executable files are used

2.gcc Common options

gcc Common options :
-v: see gcc The version of the compiler , Show gcc Detailed process of execution
-o Place the output into (file It is also an executable )
notes : Specify the output file name as file, This name cannot have the same name as the source file name
-E Preprocess only; do not compile, assemble or link
Just preprocessing , I can't compile 、 assembly 、 link
-S Compile only; do not assemble or link
Compile only , I can't compile 、 link
-c Compile and assemble, but do not link
Compile and assemble , Will not link
//==================================================
gcc -v: see gcc The version of the compiler

3. Usage mode

The way 1:( Jump in one step )
gcc hello.c Output one a.out( Executable file ), then ./a.out To execute the application .

gcc -o hello hello.c Output hello, then ./hello To execute the application .

** The way 2:( Follow the compilation process to link )** The previous files are the results of their generation
gcc -E -o hello.i hello.c ( Preprocessing )
gcc -S -o hello.s hello.i ( compile )
gcc -c -o hello.o hello.s ( assembly )
gcc -o hello hello.o ( link )
notes :.o:object file(OBJ file )
 Insert picture description here

explain : A code execution is divided into four steps
For details, see the interview classic
 Insert picture description here

Summary :
1) The input file's suffix and options are jointly determined gcc What are the operations to be performed .
2) During the compilation process , Unless you use -E、-S、-c Options ( Or a compilation error prevents the complete compilation process )
Otherwise, the last step is to link .

The way 3:
gcc -c -o hello.o hello.c( Direct use -C-O Preprocessing , compile , assembly )
gcc -o hello hello.o

gcc Would be right .c The file is preprocessed by default ,-c Let's go back to compilation 、 assembly , To get .o file
Re pass gcc -o hello hello.o take .o File links , Get the executable application .

The link is generated from the assembly OBJ file 、 System library OBJ file 、 Linking library files ,
Finally, the executable program that can run on a specific platform is generated .

crt1.o、crti.o、crtbegin.o、crtend.o、crtn.o yes gcc Join the system standard startup file ,
For general applications , These starts are required .

-lc: link libc The library files , among libc Library file printf Such as function .
gcc -v -nostdlib -o hello hello.o Will prompt because there is no link system standard startup file and standard library file , And the link failed .

This -nostdlib Options are often used on bare metal /bootloader、linux Kernel and other programs , Because they don't need startup files 、 Standard library files .

General applications only need system standard startup files and standard library files .
Bare pager /bootloader、linux Kernel and other programs do not need boot files 、 Standard library files .

Dynamic link uses dynamic link library to link , The generated program needs to load the required dynamic library to run during execution .
The program generated by dynamic link is smaller , But you have to rely on the dynamic libraries you need , Otherwise it can't be executed .

Static linking uses static libraries for linking , The generated program contains all the libraries needed to run the program , It can run directly ,
However, the program generated by static link is bulky .

gcc -c -o hello.o hello.c
gcc -o hello_shared hello.o
gcc -static -o hello_static hello.o

原网站

版权声明
本文为[Soy sauce;]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140548511309.html