Summary of common compilation commands under C and C++ compiler GCC

  • 2020-04-02 03:12:17
  • OfStack

Introduction to the
GCC stands for GUN C Compiler. Over the years, GCC has not only supported C, it now supports Ada, C++, Java, Objective C, Pascal, COBOL, and Mercury, which supports functional and logical programming. GCC no longer simply means "GUN C compiler", but "GUN Complier Collection", which means "GUN compiler family". On the other hand, when it comes to GCC support for both operating system and hardware platforms, it's all in one sentence: ubiquitous.


compile
The sample program is as follows:


  #include <stdio.h> 
   
  int main(void) { 
    printf("Hello World!n"); 
   
    return 0; 
  } 


In this program, the one-step compiler instruction is:


  gcc -o test test.c 


In essence, the above compilation process is divided into four stages:

      Preprocessing (also known as precompilation, Preprocessing)       Compilation       Assembly       Linking


pretreatment


  gcc -E test.c -o test.i  or  gcc -E test.c 


You can output the preprocessed code of test.c in the test. I file. Open the test.i file and take a look at it. The latter instruction is directly output preprocessed code in the command line window.

The -e option of GCC lets the compiler stop after preprocessing and output the preprocessing results. In this case, the result is to insert the contents of the stdio.h file into test.c

Compile into assembly code
After preprocessing, the generated test. I file can be directly compiled to generate assembly code:


  gcc -S test.i -o test.s 


The -s option of GCC means that during program compilation, after generating assembly code, stop, -o output assembly code file

assembly
For the assembly code file test.s generated in the previous section, the gas assembler is responsible for compiling it into the target file, as follows:


  gcc -c test.s -o test.o 


The connection
The GCC connector is provided by gas and is responsible for connecting the program's target file with all the additional target files required to generate the executable. Additional target files include static and dynamic connection libraries.

For the test.o generated in the previous section, connect it to the c standard I/o library to generate the program test


  gcc test.o -o test 


Execute./test in a command line window and have it say Hello World! !



Related articles: