文章目录
1 命令概述
gcc -D__DIFF_REF_KVM__ -O2 -MMD -Wall -Werror -ggdb3 -I./include -I./src/engine/interpreter -D__ENGINE_interpreter__ -D__ISA__=x86 -D__ISA_x86__ -D_ISA_H_=\"isa/x86.h\" -c -o build/obj-x86-interpreter/monitor/monitor.o src/monitor/monitor.c
This command compiles the source file src/monitor/monitor.c
with various options and generates an object file (monitor.o
) in the specified build directory. It includes preprocessor macros, sets optimization levels, enables warnings, and includes debugging information.
2 preprocessor macro
-D__DIFF_REF_KVM__
: This option defines a preprocessor macro (__DIFF_REF_KVM__
) during compilation. Preprocessor macros are used for conditional compilation in C programs.
By using the -D__DIFF_REF_KVM__
option during compilation, you effectively define the __DIFF_REF_KVM__
preprocessor macro, and this can be used in your code to conditionally include or exclude certain portions based on whether this macro is defined.
3 optimization level
-O2
: This option specifies the optimization level. In this case, it’s set to level 2, which enables a moderate level of optimization.
Optimization level, specified by the -O
option in GCC, determines the extent to which the compiler optimizes the generated machine code for performance. There are different optimization levels, ranging from -O0
(no optimization) to higher levels such as -O1
, -O2
, and -O3
. Each level introduces more aggressive optimizations, but also increases compilation time.
Choosing the appropriate optimization level depends on the specific requirements of the project. During development and debugging, using lower optimization levels like -O0
or -O1
may be preferred to ease debugging and ensure that the generated code closely corresponds to the original source. In contrast, for release builds, higher optimization levels like -O2
or -O3
are often used to produce faster and more efficient code.
4 dependency information
-MMD
: This option instructs the compiler to generate dependency information in the form of Makefile rules. This is useful for tracking dependencies between source and header files.
- Without
-MMD
:- Compiling without
-MMD
will generatemain.o
and won’t create any dependency file. - After making a modification to
functions.h
, if you runmake
again, it won’t recognize the changed header file, and you might need to manually clean and rebuild the project to pick up the changes.
- Compiling without
- With
-MMD
:- This command will generate both
main.o
and a corresponding dependency file, saymain.d
. - After making a modification to
functions.h
, if you runmake
again, it will detect the changes in the dependency file (main.d
) and automatically rebuildmain.c
, ensuring that the program reflects the updated dependencies.
- This command will generate both
注意,这里不要和makefile的原则:检查规则中的目标是否需要更新,必须先检查它的所有依赖,依赖中有任一个被更新,则目标必须更新
混淆了。
在gcc命令中,使用-I
选项(后面会继续提及)来指定编译器应该去哪里寻找头文件,而不是像makefile中写的文件依赖那样。
5 warning messages
-Wall
: This option enables a set of warning messages. It stands for “all warnings.”
-Werror
: This option treats all warning messages as errors, causing the compilation to fail if any warnings are issued.
6 debugging information
-ggdb3
: This option includes debugging information in the executable. The argument ggdb3
specifies the level of detail for debugging information, and in this case, it’s set to level 3.
The -ggdb3
option in the gcc command is used to include debugging information in the generated executable, specifically in the format that is compatible with the GDB (GNU Debugger) tool. This information allows you to debug your program effectively using GDB, providing details about variables, function names, and source code lines during the debugging process.
-g
option: This is the main option for including debugging information. It tells the compiler to generate debugging information in the output file. The level of detail provided by-g
can vary, and using a specific level such as-g3
or-ggdb3
ensures a higher level of detail, useful for debugging with GDB.-ggdb3
specifically: This option is an extension of the-g
option and provides additional debugging information specific to GDB. The 3 indicates a higher level of detail compared to-g
or-ggdb
alone.
7 where to look for header files
-I./include
-I./src/engine/interpreter
: These options specify the paths to include directories where header files are located.
In this command, there are two instances of the -I
option, each followed by a directory path. The -I
option is used to specify the directories where the compiler should look for header files when processing #include
directives in the source code.
8 其他
-c
: This option instructs the compiler to generate object files but not perform the linking step.
-o build/obj-x86-interpreter/monitor/monitor.o
: This option specifies the output file for the compilation. In this case, it’s an object file named monitor.o
located in the specified build directory.
src/monitor/monitor.c
: This is the source file that will be compiled.