Detailed Explanation of GCC Parameters
Category Programming Technology
gcc and g++ are the C and C++ compilers of GNU respectively. When gcc/g++ perform compilation, there are a total of 4 steps required:
- Preprocessing, generating files with the .i extension [Preprocessor cpp]
- Converting the preprocessed files into assembly language, generating files with the .s extension [Compiler egcs]
- Transforming assembly into object code (machine code), generating files with the .o extension [Assembler as]
- Linking object code to generate an executable program [Linker ld]
Detailed Explanation of Parameters
-x language filename
Sets the language used in the file, making the file extension invalid, and this is effective for multiple subsequent files. That is, by convention, the suffix for C language files is .c, and for C++ it is .C or .cpp. If you decide to use a unique suffix like .pig for your C code file, you will need to use this parameter. This parameter will affect the filenames that follow it until the next parameter is used. The available options include: 'c', 'objective-c', 'c-header', 'c++', 'cpp-output', 'assembler', and 'assembler-with-cpp'.
Seeing the English, it should be understandable.
Example usage:
gcc -x c hello.pig
-x none filename
Turns off the previous option, which allows gcc to automatically recognize the file type based on the file name suffix.
Example usage:
gcc -x c hello.pig -x none hello2.c
-c
Only activates preprocessing, compilation, and assembly, meaning it only turns the program into an object file.
Example usage:
gcc -c hello.c
It will generate an object file with the .o extension.
-S
Only activates preprocessing and compilation, which means compiling the file into assembly code.
Example usage:
gcc -S hello.c
It will generate assembly code with the .s extension, which you can view with a text editor.
-E
Only activates 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 your time to look, even a "hello world" can be processed into 800 lines of code.
-o
Specifies the target name, by default, the file compiled by gcc is a.out, which is not very pleasant to hear. If you feel the same way as me, change it, haha.
Example usage:
gcc -o hello.exe hello.c (oh, used to Windows)
gcc -o hello.asm -S hello.c
-pipe
Uses pipes instead of temporary files during compilation, which may have some issues when using non-GNU assembly tools.
gcc -pipe -o hello.exe hello.c
-ansi
-fno-asm
This option implements part of the functionality of the ansi option, which prohibits the use of asm, inline, and typeof as keywords.
-fno-strict-prototype
Only effective for g++, with this option, g++ will consider functions without parameters as not having explicit declarations of the number and type of parameters, rather than having no parameters.
And gcc, regardless of whether this parameter is used or not, will consider functions without parameters as having no explicit type declarations.
-fthis-is-variable
It is to align with traditional C++, allowing the use of this as a general variable.
-fcond-mismatch
Allows the second and third arguments of a conditional expression to have mismatched types, and the value of the expression will be of void type.
-funsigned-char 、-fno-signed-char、-fsigned-char 、-fno-unsigned-char
These four parameters are used to set the char type, deciding whether to set the char type as unsigned char (the first two parameters) or signed char (the last two parameters).
-include file
Includes a piece of code, in simple terms, it is to use a certain file when another file is needed, and it can be set with it, and its function is equivalent to using #include<filename>
in the code.
Example usage:
gcc hello.c -include /root/pianopan.h
-imacros file
Expands the macros of the file to the input files of gcc/g++, and the macro definition itself does not appear in the input file.
-Dmacro
Equivalent to #define macro
in the C language.
-Dmacro=defn
Equivalent | -O2 | Further optimization. | | -O3 | Further optimization beyond -O2, including inline functions. | | -shared | Generate shared object files. Commonly used when building shared libraries. | | -static | Prohibit the use of shared connections. | | -UMACRO | Undefine the macro MACRO. | | -w | Do not generate any warning messages. | | -Wall | Generate all warning messages. |