In Linux, we usually use gcc to compile C code into executable files. The following is a simple example:
Code File: hello. c
# Include
<Stdlib. h>
# Include
<Stdio. h>
Void main (void)
{
Printf ("hello world! \ R \ n ");
}
You can use the following command to compile an executable file:
Gcc hello. c
After the command is executed, an executable file a. out is obtained.
Compilation process
The previous example just briefly introduced the usage of gcc. Anyone familiar with c programming will know, this step actually contains four steps: preprocessing --> Compilation --> assembly --> link. The functions implemented in these four steps are as follows:
- Preprocessing phase:It mainly processes # ifdef, # include, and # define commands in the source file, expands macros, and reads the defined symbols.
- Compilation phase:Check the code standardization and translate the code into an assembly language.
- Assembly phase:Is to convert the. s file generated in the compilation phase to the binary target code
- Link stage:Combine the machine code generated in the assembly phase into an executable binary code file.
It can be seen that the output of each stage is actually the input of the next stage. With gcc, you can perform these four steps separately:
Gcc-E hello. c-o hello. I
Gcc-S hello. I-o hello. s
Gcc-c hello. s-o hello. o
Gcc hello. o-o hello.exe
In fact, because these four steps are too complex, they can always be executed in a single command as I did above:
Gcc hello. c
-O hello.exe
Here I add a-o parameter to specify the output name, instead of the default a. out parameter.
If there are multiple files, you can set them all in the following way.
Gcc-o test first. c second. c third. c
This fully-generated method is very simple, but there is a problem: when the project is large, if only one file is changed, you still need to re-compile the index file.
To solve this problem, we often split the compilation process into two steps:
- Compile each. c file into a. o file.
- Link all. o files to the execution File
Gcc-c first. c
Gcc-c second. c
Gcc-c third. c
Gcc-o test first. o second. o third. o
In this way, when third. when the c file is changed, you only need to re-compile third. c and the link, which saves the Compilation Time of unchanged files, that is, the incremental compilation we usually call.
Gcc-c third. c
Gcc-o test first. o second. o third. o
We can also see from the above usage:
- When the-c parameter is used, if the input file is. c, the pre-processing, compilation, and assembly stages are executed at the same time, and the. o file is directly generated.
- When the input file is. o, you can directly perform the link operation.
Since programmers often do not care about the output files generated in the previous two phases, we usually combine the three phases of preprocessing, compilation, and assembly, collectively referred to as compilation and input. c. generate. o.
Common parameters:
We have already demonstrated the usage of-E,-S,-c,-o and other parameters. Among them,-E and-S are rarely used, and-c is used for compilation and generation. o file,-o is used to specify the name of the output file. In addition to the generation and control parameters, there are many parameter settings. Here we mainly introduce several common parameters:
Including header files and libraries:
- -Idir:Specifies the directory for compiling and searching the header file. It is often used to find the header file of a third-party library, for example, gcc test. c-I ../inc-o test.
- -Ldir: Find the lib directory when specifying a link. It is often used to find a third-party library.
- -Llibrary: Specify the lib library for the additional link
Macro definition:
- -DMACRO defines the MACRO with the string "1" (default.
- -DMACRO = DEFN defines MACRO macros using the string "DEFN". Note that there must be no spaces in the middle.
- -UMACRO undefines MACRO macros.
Debugging and executable file format:
- -G indicates the compiler to generate debugging information during compilation.
- -Ggdb generates as much debugging information as possible for gdb (more information than-g ).
- -Static prohibit the use of dynamic libraries. The compiled program is large but can run freely.
- -Share uses dynamic libraries whenever possible, so the file generation is small, but the system must use dynamic libraries.
Alarm options:
- -Wall generates as many warning messages as possible. We recommend that you always include
- -Werror treats all warnings as errors.
Gcc and g ++
In addition to the gcc compiler, there is another compiler g ++. Many people often cannot figure out the differences between the two compilers. Many people think that gcc can only compile c code, g ++ can only compile c ++ code. In fact, the main differences between the two compilers are as follows:
- The suffix is. c. gcc treats it as a C program, while g ++ compiles it as a c ++ program.
- Gcc does not add the-lstdc ++ option (g ++) by default during the link, but we often do not have the habit of manually adding this option, in this case, when the stl library is used for compiling c ++ code by gcc, the link fails.
For more information on their differences, refer to this article: http://ldzyz007.iteye.com/blog/865080
For ease of use, you can develop the following rules: Use gcc for c Projects and use g ++ for c ++ projects. It is estimated that the intention to design this name was the same at that time.