About GCC
GCC (GNU Compiler Collection) is a powerful, performance-rich programming language compiler, one of the GNU program's representative works. GCC is issued under the GPL and LGPL license, it is a standard compiler for UNIX-like and Mac OS X operating systems
In addition to supporting C, it also supports C + +, Java
GCC Common options
1. Basic options
Type |
Description |
-E |
Stop after preprocessing, do not compile, assemble, and connect |
-S |
Stop after compiling, do not assemble and connect |
-C |
Compile or assemble source files, but do not connect |
-O File |
Specifies that the output file |
Example:
GCC-E-o test.i test.c Edit the test.c file (-e instructions stop after preprocessing, do not compile, assemble, and connect), and then output the pre-processing output to test.i (via-o instructions)
Gcc-s-O test.s test.i files that were preprocessed from the previous step are test.i compiled into assembly language Test.s
Gcc-c Test.s-o TEST.O compiles the previous assembly file into the target file, but does not connect (so it cannot be performed)
Gcc-o test TEST.O Connect the target file that was compiled in the previous step to generate the executable file test
The above approach from preprocessing, assembly, connection step down, but in the real development environment is a step from the. C source file compiled into an executable file:gcc-o test test.c
2. Warning Selection,
In the process of compiling the program, the compiler error and warning information is very important to the programmer. GCC includes complete error checking and warning prompts to help Linux programmers find the wrong or potential error code as quickly as possible to write more professional and graceful code
Type |
Description |
-wall |
Start all warning messages |
-werror |
Cancels the compilation operation when a warning occurs, and the warning is considered an error |
-W |
Disable all warning messages |
Example: Gcc-wall-o test test.c Displays all warning messages when compiling test.c files
3. Optimization options
-o0: No optimization processing
-O or-o1: Basic optimization, which in most cases makes the program perform faster
-o2: In addition to completing-O1 level optimizations, there are additional tuning tasks, such as processor instruction scheduling, which is the default optimization level for GNU release software
-o3: In addition to completing-O2 level optimizations, loop unwinding and other optimizations related to processor characteristics
-os: Generates the smallest executable file, mainly used in the embedded domain
In general, the higher the level of optimization, the faster the resulting executable will run, but the longer it takes to compile, so try not to use the optimization option at the time of development, and consider the final optimization of the code at the end of the software release or development. Recommended use of-o2
Here is a command to test the execution time of a program: The Timesexecutable file (for example: Time./test), and a message similar to the following will be displayed
Real 0m1.206s// Total execution time (including scheduling and switching of processes) user 0m1.172s// subscriber state Execution time SYS 0m0.018s //kernel state execution time
4. Connector options
header file : The declaration part of the main containing function, excluding the specific definition of the function
Library file : The specific implementation of the function is done in the library file.
Static library files: When compiling a link, the source code of the static library file is added to the executable file, the runtime is used directly, but the resulting executable file is generally larger (the suffix of the static library file is. a)
Dynamic Library files: When a link is compiled, the dynamic library file does not add the source code to the executable file, but instead loads the dynamic library file by the runtime's connection file when the program executes, which is more cost-saving. (The suffix of the dynamic library file is. So)
Type |
Description |
-idirectory |
Add a new directory to the GCC header file search path |
-ldirectory |
Add a new directory to the GCC library file search path |
-llibrary |
Prompts the connector to include the specified library file when creating the executable file, for example-LM to connect to the specified math library |
-static |
Force the use of the static link library, because the default is to use the dynamic-link library |
-shared |
Generate a dynamic library file |
Example: (filename is test.c)
#include <stdio.h> #include <math.h>int main () { int i=1; printf ("%d\b", Sin (i)); return 0;}
Compile the source code with Gcc-o test test.c, but there is an error when connecting to the program, so it cannot be compiled correctly
The main reason is that in C, only the standard library of the C language is connected by default (the path to the standard library is/usr/lib/libc.so), but the library file math.h for the header file is not in the standard library, but in/usr/lib/libm.so, so it needs to be explicitly specified.
So you should use this command: Gcc-o Test test.c/usr/lib/libm.so. This can be compiled by
Or use Gcc-o test TEST.C-LM. The-LM option here is to indicate that the math library is specified
5. Other options
Type |
Description |
-X language |
Specifies the programming language of the input file (the default GCC identifies the source file based on the suffix of the source file, but can also be explicitly specified via-X) |
-V |
Displays the version number of the compiler |
-G |
Get more information about the debugger, which you need to use with GDB, see next blog post |
-ansi |
Support for ANSI compliant C programs |
Linux C Programming Learning 2---GCC compilers