Linux: The use and difference between gcc and g++

What is gcc and g++

gcc: GNU C Compiler (C compiler) in GCC Note: GCC and gcc are two things

g++: is the GNU C++ Compiler in GCC (C++ compiler)

GCC: GNU Compiler Collection (GNU Compiler Collection), it can compile C, C++, JAV, Fortran, Pascal, Object-C, Ada and other languages.

The difference between gcc and g++

Both gcc and g++ can compile c language and c++

If it is a .c file, gcc will be compiled in the way of c language, and g++ will be compiled in the way of c++; if it is a .cpp file, both gcc and g++ will be compiled in the way of c++

gcc will only connect to the c library by default, not to the c++ library, and g++ will connect. When gcc compiles the cpp file, you must manually connect to the c++ library (command: gcc -o main main.cpp -lstdc++)

Use of gcc/g++

gcc is used in the same way as g++, here only gcc is taken as an example

1. Pre-compilation

gcc -E **.c  #执行命令后生成**.i文件

Pre-compilation stage: processing pre-compilation instructions, delete comments, macro replacement

2. Compile

gcc -S **.i  #执行命令后默认生成一个**.s文件

Compilation stage: grammar, lexical analysis, code optimization, summary symbols

3. Compilation

gcc -c **.s  #执行命令后生成**.o文件,是可重定位的二进制文件

Assembly stage: Translate into binary, generate each segment, generate symbol table

4. Link

gcc **.o  #默认生成一个a.out文件,a.out是可执行文件

Linking stage: merge each segment, symbol analysis, symbol relocation

The above command can add -o to specify the name of the generated file, o is the abbreviation of output, for example:

gcc **.o -o 自定义的文件名  #指定生成的可执行文件名称

The above process is summarized as follows
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/huifaguangdemao/article/details/108086587