gcc and g++ are GNU's C & C++ compilers. When gcc/g++ performs compilation, it takes 4 steps in total:
- 1. Preprocessing, generating .i files [preprocessor cpp]
- 2. Convert the preprocessed file into assembly language, generating .s files [compiler egcs]
- 3. Convert assembly into object code (machine code), generating .o files [assembler as]
- 4. Link the object code to generate an executable program [linker ld]
Parameter Details
-x language filename
Set the language used by the file, invalidating the suffix name, effective for multiple subsequent files. That is, by convention, the suffix for C language is .c, and the suffix for C++ is .C or .cpp. If you are very individualistic and decide your C code file suffix is .pig, haha, then you need to use this parameter. This parameter takes effect on all file names after it, until the next parameter is used. The parameters that can be used are the following: 'c', 'objective-c', 'c-header', 'c++', 'cpp-output', 'assembler', and 'assembler-with-cpp'.
Seeing the English, you should understand.
Example usage:
gcc -x c hello.pig
-x none filename
Turn off the previous option, that is, let gcc automatically identify the file type based on the file name suffix.
Example usage:
gcc -x c hello.pig -x none hello2.c
-c
Only activate preprocessing, compilation, and assembly, that is, it only turns the program into an obj file.
Example usage:
gcc -c hello.c
It will generate an .o obj file.
-S
Only activate preprocessing and compilation, that is, compile the file into assembly code.
Example usage:
gcc -S hello.c
It will generate .s assembly code, which you can view with a text editor.
-E
Only activate preprocessing; this does not generate a file. You need to redirect it to an output file.
Example usage:
gcc -E hello.c > pianoapan.txt gcc -E hello.c | more
Take a look slowly; a hello world also gets preprocessed into 800 lines of code.
-o
Specify the target name. By default, the file compiled by gcc is a.out, which is ugly. If you feel the same as I do, change it, haha.
Example usage:
gcc -o hello.exe hello.c (哦,windows用习惯了) gcc -o hello.asm -S hello.c
-pipe
Use pipes instead of temporary files during compilation. There may be some problems when using non-GNU assembly tools.
gcc -pipe -o hello.exe hello.c
-ansi
Turn off features in GNU C that are incompatible with ANSI C, and activate ANSI C's proprietary features (including prohibiting some keywords like asm, inline, typeof, as well as preprocessing macros such as UNIX, vax, etc.).-fno-asm
This option implements part of the functionality of the ansi option; it prohibits using asm, inline, and typeof as keywords.
-fno-strict-prototype
Only effective for g++. With this option, g++ will treat functions without parameters as having no explicit declaration of the number and type of parameters, rather than as having no parameters.
And gcc, whether or not this parameter is used, will treat functions without parameters as having no explicit type declaration.
-fthis-is-varialble
That is, to align with traditional C++, allowing this to be used as an ordinary variable.
-fcond-mismatch
Allow the second and third parameter types of a conditional expression to not match; the value of the expression will be of type void.
-funsigned-char 、-fno-signed-char、-fsigned-char 、-fno-unsigned-char
These four parameters are for setting the char type, determining whether to set char to unsigned char (first two parameters) or signed char (last two parameters).
-include file
Include a certain code. Simply put, when compiling a certain file and needing another file, you can use it to set that. Its function is equivalent to using in the code:#include<filename>。
Example usage:
gcc hello.c -include /root/pianopan.h
-imacros file
Expand the macros of the file into the gcc/g++ input file; the macro definitions themselves do not appear in the input file.
-Dmacro
Equivalent to in C language:#define macro
-Dmacro=defn
Equivalent to in C language:#define macro=defn
-Umacro
Equivalent to in C language:#undef macro
-undef
Cancel the definition of any non-standard macros.
-Idir
When you use #include "file"gcc/g++ will first look for the header file you specified in the current directory. If not found, it goes back to the default header file directory. If you use -I to specify a directory, it will first search in the directory you specified, then search in the normal order.
For #include<file>, gcc/g++ will search in the directory specified by -I; if not found, it will then search in the system's default header file directory.
-I-
It cancels the function of the previous parameter, so it is generally used after -Idir.
-idirafter dir
If searching in the -I directory fails, it will search in this directory.
-iprefix prefix 、-iwithprefix dir
Generally used together; when searching in the -I directory fails, it will search under prefix+dir.
-nostdinc
Make the compiler no longer search for header files in the system default header file directory; generally used together with -I to explicitly limit the location of header files.
-nostdin C++
Specify not to search in the standard paths designated by g++, but still search in other paths. This option is used when creating the libg++ library.
-C
Do not delete comment information during preprocessing; generally used with -E. Sometimes it is very convenient for analyzing programs.
-M
Generate file dependency information. It includes all source code that the target file depends on. You can usegcc -M hello.cto test it; it's very simple.
-MM
Same as the one above, but it will ignore the#include<file>dependency relationships caused by this.
-MD
Same as -M, but the output will be directed into a .d file.
-MMD
Same as -MM, but the output will be directed into a .d file.
-Wa,option
This option passes option to the assembler; if there is a comma in option, it splits option into multiple options, then passes them to the assembler.
-Wl.option
This option passes option to the linker; if there is a comma in option, it splits option into multiple options, then passes them to the linker.
-llibrary
Specify the library used during compilation.
Example usage
gcc -lcurses hello.c
Compile the program using the ncurses library.
-Ldir
Specify the path to search for libraries during compilation. For example, for your own libraries, you can use it to specify the directory; otherwise, the compiler will only search in the standard library directories. This dir is the name of the directory.
-O0 、-O1 、-O2 、-O3
The 4 levels of compiler optimization options: -O0 means no optimization, -O1 is the default, and -O3 is the highest optimization level.
-g
Just for the compiler, generate debugging information during compilation.
-gstabs
This option generates debugging information in stabs format, but does not include gdb debugging information.
-gstabs+
This option generates debugging information in stabs format, and includes additional debugging information for use only by gdb.
-ggdb
This option will generate as much debugging information usable by gdb as possible.
-static
This option will prohibit the use of dynamic libraries, so the compiled output is generally very large and can run without needing any dynamic link libraries.
-share
This option will try to use dynamic libraries, so the generated file is smaller, but the system needs to have dynamic libraries.
-traditional
Attempt to make the compiler support traditional C language features.
GCC is GNU's C and C++ compiler. In fact, GCC can compile three languages: C, C++, and Object C (an object-oriented extension of C). Using the gcc command, you can compile and link C and C++ source programs at the same time.
If you have two or a few C source files, you can also conveniently use GCC to compile, link, and generate an executable file. For example, suppose you have two source files, main.c and factorial.c, and now you want to compile and generate a program that calculates factorials.
factorial.c file code
main.c file code
Use the following command to compile and generate an executable file, and run the program:
$ gcc -o factorial main.c factorial.c $ ./factorial 5 Factorial of 5 is 120.
GCC can be used to compile both C programs and C++ programs. Generally, the C compiler determines whether it is a C program or a C++ program by the suffix of the source file. In Linux, the suffix of C source files is .c, while the suffix of C++ source files is .C or .cpp. However, the gcc command can only compile C++ source files and cannot automatically link with the libraries used by C++ programs. Therefore, the g++ command is usually used to complete the compilation and linking of C++ programs; this program automatically calls gcc to implement compilation. Suppose we have the following C++ source file (hello.c):
hello.c file code
Then you can call the g++ command as follows to compile, link, and generate an executable file:
$ g++ -o hello hello.c $ ./hello Hello, world!
Common options of the gcc command
| Option | Explanation |
|---|---|
| -ansi | Only supports C syntax of the ANSI standard. This option will prohibit certain features of GNU C, such as asm or typeof keywords. |
| -c | Only compile and generate object files. |
| -DMACRO | Define the MACRO macro with the string "1". |
| -DMACRO=DEFN | Define the MACRO macro with the string "DEFN". |
| -E | Only run the C preprocessor. |
| -g | Generate debugging information. The GNU debugger can use this information. |
| -IDIRECTORY | Specify an additional header file search path DIRECTORY. |
| -LDIRECTORY | Specify an additional library search path DIRECTORY. |
| -lLIBRARY | Search the specified library LIBRARY when linking. |
| -m486 | Optimize code for the 486. |
| -o FILE | Generate the specified output file. Used when generating executable files. |
| -O0 | Do not perform optimization. |
| -O or -O1 | Optimize the generated code. |
| -O2 | Optimize further. |
| -O3 | Optimize further than -O2, including inline functions. |
| -shared | Generate shared object files. Usually used when building shared libraries. |
| -static | Disallow shared linking. |
| -UMACRO | Undefine the MACRO macro. |
| -w | Do not generate any warning messages. |
| -Wall | Generate all warning messages. |