gcc编译的命令行选项
一些重要的gcc命令行选项
https://gcc.gnu.org/onlinedocs/ 各个不同版本的gcc编译器
https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html 命令行选项汇总页
GCC 版本 | C语言常用标准 | |||||||
---|---|---|---|---|---|---|---|---|
C89/C90 | C99 | C11 | C17 | GNU90 | GNU99 | GNU11 | GNU17 | |
10.1 ~ 8.4 | c89 / c90 | c99 | c11 | c17/c18 | gnu90/gnu89 | gnu99 | gnu11 | gnu17/gnu18 |
7.5 ~ 5.5 | c89/c90 | c99 | c11 | gnu90/gnu89 | gnu99 | gnu11 | ||
4.9.4 ~ 4.8.5 | c89/c90 | c99 | c11 | gnu90/gnu89 | gnu99 | gnu11 | ||
4.7.4 | c89/c90 | c99(部分支持) | c11(部分支持) | gnu90/gnu89 | gnu99(部分支持) | gnu11(部分支持) | ||
4.6.4 | c89/c90 | c99(部分支持) | c1x(部分支持) | gnu90/gnu89 | gnu99(部分支持) | gnu1x(部分支持) | ||
4.5.4 | c89/c90 | c99(部分支持) | gnu90/gnu89 | gnu99(部分支持) |
GCC 版本 | C++常用标准 | |||||||
---|---|---|---|---|---|---|---|---|
C++98/03 | C++11 | C++14 | C++17 | GNU++98 | GNU++11 | GNU++14 | GNU++17 | |
10.1 ~ 8.4 | c++98/c++03 | c++11 | c++14 | c++17 | gnu++98/gnu++03 | gnu++11 | gnu++14 | gnu++17 |
7.5 ~ 5.5 | c++98/c++03 | c++11 | c++14 | c++1z(部分支持) | gnu++98/gnu++03 | gnu++11 | gnu++14 | gnu++1z(部分支持) |
4.9.4 ~ 4.8.5 | c++98/c++03 | c++11 | c++1y(部分支持) | gnu++98/gnu++03 | gnu++11 | gnu++1y(部分支持) | ||
4.7.4 | c++98 | c++11(部分支持) | gnu++98 | gnu++11(部分支持) | ||||
4.6.4 | c++98 | c++0x(部分支持) | gnu++98 | gnu++0x(部分支持) | ||||
4.5.4 | c++98 | c++0x(部分支持) | gnu++98 | gnu++0x(部分支持) |
具体
See Option Index, for an index to GCC’s options.
• Option Summary: | Brief list of all options, without explanations. | |
• Overall Options: | Controlling the kind of output: an executable, object files, assembler files, or preprocessed source. | |
• Invoking G++: | Compiling C++ programs. | |
• C Dialect Options: | Controlling the variant of C language compiled. | |
• C++ Dialect Options: | Variations on C++. | |
• Objective-C and Objective-C++ Dialect Options: | Variations on Objective-C and Objective-C++. | |
• Diagnostic Message Formatting Options: | Controlling how diagnostics should be formatted. | |
• Warning Options: | How picky should the compiler be? | |
• Static Analyzer Options: | More expensive warnings. | |
• Debugging Options: | Producing debuggable code. | |
• Optimize Options: | How much optimization? | |
• Instrumentation Options: | Enabling profiling and extra run-time error checking. | |
• Preprocessor Options: | Controlling header files and macro definitions. Also, getting dependency information for Make. | |
• Assembler Options: | Passing options to the assembler. | |
• Link Options: | Specifying libraries and so on. | |
• Directory Options: | Where to find header files and libraries. Where to find the compiler executable files. | |
• Code Gen Options: | Specifying conventions for function calls, data layout and register usage. | |
• Developer Options: | Printing GCC configuration info, statistics, and debugging dumps. | |
• Submodel Options: | Target-specific options, such as compiling for a specific processor variant. | |
• Spec Files: | How to pass switches to sub-processes. | |
• Environment Variables: | Env vars that affect GCC. | |
• Precompiled Headers: | Compiling a header once, and using it many times. | |
• C++ Modules: | Experimental C++20 module system. |
常用选项 | 解释 |
-I- | Split the include path. Any directories specified with '-I' options before '-I-' are searched only for headers requested with #include "file" ; they are not searched for #include <file> . If additional directories are specified with '-I' options after the '-I-', those directories are searched for all #include directives.In addition, '-I-' inhibits the use of the directory of the current file directory as the first search directory for #include "file" . |
-q |
组装和连接时尽量保持安静。编译器不需要此选项因为它总是安静的,但是链接器,特别是汇编器输出一些信息。 |
-g | GCC中的“-g”参数打开调试gcc verbose模式。在TIGCC中,这是DWARF 2,其可与启用GDB的TiEmu一起使用。 |
-ar | Create an archive (a static library) instead of a program. The resulting file will have an .a ending. |
-bsr | This option is ignored for compatibility.In addition to these options, the tigcc program also accepts most of the linker options. |
-c | 编译或汇编源文件,但不链接。链接阶段根本没有完成。最终输出是每个源文件的对象文件形式。 默认情况下,源文件的对象文件名是通过将后缀.c、.i、.s等替换为.o文件。 忽略不需要编译或程序集的无法识别的输入文件。 即 .c-->.i-->.s-->.o 并没有到衔接生成的二进制文件 |
-C | 不要丢弃备注。所有注释都传递到输出文件,但已处理指令中的注释除外,这些注释将随指令一起删除。 使用“-C”时,应做好产生影响的准备;它使预处理器将注释视为自己的标记。例如出现在指令行开头的注释具有将该行转换为普通源行的效果,因为该行上的第一个标记不再是#。 |
-CC | 不要丢弃注释,包括在宏展开期间。这类似于“-C”,只是宏中包含的注释也会传递到宏展开的输出文件。 In addition to the side-effects of the '-C' option, the '-CC' option causes all C++-style comments inside a macro to be converted to C-style comments. This is to prevent later use of that macro from inadvertently commenting out the remainder of the source line. “-CC”选项通常用于支持lint注释。 |
-S | 编译阶段结束后停止;不要组装。输出以指定的每个非汇编程序输入文件的汇编程序代码文件的形式。 即生成.s汇编文件即停止(.c -->.i-->.s) 默认情况下,源文件的汇编程序文件名是通过将后缀.c、.i等替换为.s来生成的。 忽略不需要编译的输入文件。 |
-E |
预处理阶段后停止;不要正确运行编译器。输出是经过预处理的源代码的形式,它被发送到标准输出。 忽略不需要预处理的输入文件。即 |
-o file | 将输出放在文件文件中。这适用于正在产生的任何类型的输出, 无论是可执行文件、目标文件、汇编文件还是预处理的C代码。 由于只能指定一个输出文件,因此在编译多个输入文件时使用“-o”没有意义,除非您正在生成可执行文件作为输出。 如果未指定“-o”,则默认值是将可执行文件放入.out, source.o中source.suffix的对象文件,source.s中的汇编程序文件,以及标准输出上的所有预处理C源。 |
-v | Print (on standard error output) the commands executed to run the stages of compilation. Also print the version number of the compiler driver program and of the preprocessor and the compiler proper. |
-lc | 表示需要链接libc 库,-lc 选项是gcc 默认的,不用写,而对于ld 则不是默认选项,所以要写上 |
-std=standard |
Determine the language standard. A value for standard must be provided; provided; possible values are c89
iso9899:199409
c99
gnu89
gnu99
Even when this option is not specified, you can still use some of the features of newer standards |
-w |
禁用所有警告消息。 |
-W |
打印这些事件的额外警告消息: |
-Wall |
输出一系列警告信息 |
-include file |
Process file as if |
-Ldir |
-l , 将目录 dir 添加到要搜索的目录列表中。 |
-llibrary |
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.) ld 链接时搜索名为library的库。(将库作为单独参数的第二种选择仅遵从于POSIX,不建议使用。) -l选项由GCC直接传递给链接器。有关详细信息,请参阅链接器文档。下面的一般描述适用于GNU链接器。 The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L. Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used. It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded. |
-lobjc |
You need this special case of the -l option in order to link an Objective-C or Objective-C++ program. |
-static |
在支持动态链接的系统上,这会覆盖-pie并阻止与共享库的链接. 在其他系统上,此选项无效 |
-shared |
Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you specify this linker option.1 |
-shared-libgcc -static-libgcc |
On systems that provide libgcc as a shared library, these options force the use of either the shared or static version, respectively. If no shared version of libgcc was built when the compiler was configured, these options have no effect. There are several situations in which an application should use the shared libgcc instead of the static version. The most common of these is when the application wishes to throw and catch exceptions across different shared libraries. In that case, each of the libraries as well as the application itself should use the shared libgcc. Therefore, the G++ driver automatically adds -shared-libgcc whenever you build a shared library or a main executable, because C++ programs typically use exceptions, so this is the right thing to do. If, instead, you use the GCC driver to create shared libraries, you may find that they are not always linked with the shared libgcc. If GCC finds, at its configuration time, that you have a non-GNU linker or a GNU linker that does not support option --eh-frame-hdr, it links the shared version of libgcc into shared libraries by default. Otherwise, it takes advantage of the linker and optimizes away the linking with the shared version of libgcc, linking with the static version of libgcc by default. This allows exceptions to propagate through such shared libraries, without incurring relocation costs at library load time. However, if a library or main executable is supposed to throw or catch exceptions, you must link it using the G++ driver, or using the option -shared-libgcc, such that it is linked with the shared libgcc. |
文件后缀讲解
https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options
file.c | C source code which must be preprocessed. |
file.s | GNU Assembler code. |
file.S | GNU Assembler code which must be preprocessed. |
file.asm | A68k Assembler code. |
file.o | COFF object file to be linked using ld . |
file.a | Static library (function archive) to be linked using ld .You can specify the input language explicitly with the '-x' option: |
我们用实际例子来演示
1)下面举例 , 统一用下面的 main.c 文件
main.c #include <stdio.h> int main(){ for(int i=0;i<10;i++){ printf("i=%d ",i); } }
2)比较常用的命令行参数:
参数 | 说明 |
---|---|
-c | 只编译不链接,生成*.o 文件 |
-S | 生成汇编代码*.s 文件 |
-E | 预编译 生成*.i 文件 |
-g | 在可执行程序里包含了调试信息,可用 gdb 调试 |
-o | 把输出文件输出到指定文件里 |
-static | 链接静态链接库 |
-library | 链接名为library的链接库 |
3)程序编译过程分四个阶段:
1.预处理(展开宏,头文件,检查代码是否有误)
2.编译(将.c转为汇编代码.s)
3.汇编(将汇编代码.s转为机器代码.o)
4.链接(将所有机器代码.o和库文件链接成一个可执行程序)
具体命令行选项使用
一、gcc -std
gcc/g++选择的编译标准 选项-std=编译标准,用法
//编译不通过 C:\Program Files\Go\src\democgo\pointerfunc\std>gcc -std=c89 main.c main.c: In function 'main': main.c:4:5: error: 'for' loop initial declarations are only allowed in C99 or C11 mode for(int i=0;i<10;i++){ ^~~ main.c:4:5: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code //编译通过 C:\Program Files\Go\src\democgo\pointerfunc\std>gcc -std=c99 main.c
在编写程序前必须明确要使用的编译标准,并清楚得知道该标准下什么可用,什么不可用
二、 gcc -v (windows)
在gcc中,我们使用的gcc.exe和g++.exe命令,以及其他语言的命令(gccgo),都被称作 compiler driver。他们本身并不做具体的编译工作,
而是用来解析命令行参数,调用具体的编译器和其他工具来驱动整体的编译过程。
gcc和g++只是compiler driver,而真正的c和c++编译器是另外两个可执行程序,cc1和cc1plus
无论使用gcc或者g++,我们都可以通过-v参数显示整体构建过程。
在这个过程中我们会看到对于真正的编译器cc1(c compiler)或者cc1plus(c++ compiler)的调用,以及对于as.exe(汇编器)和collect2(连接器)的调用。
gcc和g++可执行程序都使用gcc.c中的main函数作为入口函数。他们真正的区别在于gcc包含了gccspec.c文件,而g++包含了g++spec.c文件。在这两个文件中,分别实现了lang_specific_driver函数。这个函数被这个compiler driver框架用来在命令行参数处理前添加特殊的标志。对于stdc++的默认连接就是在g++spec.c中处理的
gcc.exe,as.exe等都位于
E:\mingw64\bin
cc1,collect2.exe都位于以下目录
E:\mingw64\libexec\gcc\x86_64-w64-mingw32\7.3.0\
具体过程:
C:\Program Files\Go\src\democgo\pointerfunc\std>gcc -std=c99 main.c -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=E:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../../../src/gcc-7.3.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w 64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64 --enable-shared --enabl e-static --enable-targets=all --enable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enabl e-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fu lly-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --enable-sjlj-exceptions --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable- nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch-32=i686 --with-arch-64=nocona --with-tune -32=generic --with-tune-64=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw730/prerequisites/x86_64-w64-ming w32-static --with-mpfr=/c/mingw730/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw730/prerequisites/x86_64-w 64-mingw32-static --with-isl=/c/mingw730/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-sjlj-re v0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I /c/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64/opt/include -I/c/mingw730/prerequisites/x86_64-zlib-static/include -I/c/mingw730/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw730/x86_64-730-p osix-sjlj-rt_v5-rev0/mingw64/opt/include -I/c/mingw730/prerequisites/x86_64-zlib-static/include -I/c/mingw730/prerequisi tes/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64/opt/include -I/ c/mingw730/prerequisites/x86_64-zlib-static/include -I/c/mingw730/prerequisites/x86_64-w64-mingw32-static/include' LDFLA GS='-pipe -fno-ident -L/c/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64/opt/lib -L/c/mingw730/prerequisites/x86_64-z lib-static/lib -L/c/mingw730/prerequisites/x86_64-w64-mingw32-static/lib ' Thread model: posix gcc version 7.3.0 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) COLLECT_GCC_OPTIONS='-std=c99' '-v' '-mtune=core2' '-march=nocona' E:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/cc1.exe -quiet -v -iprefix E:/mingw64/bin/../lib/gcc/x86_64-w64- mingw32/7.3.0/ -D_REENTRANT main.c -quiet -dumpbase main.c -mtune=core2 -march=nocona -auxbase main -std=c99 -version -o C:\tmp\ccfVBdfb.s GNU C99 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) version 7.3.0 (x86_64-w64-mingw32) compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.18- GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring duplicate directory "E:/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/7.3.0/include" ignoring nonexistent directory "C:/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64C:/msys64-2/mingw64/lib/gcc/x86_64-w 64-mingw32/7.3.0/../../../../include" ignoring duplicate directory "E:/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/7.3.0/include-fixed" ignoring duplicate directory "E:/mingw64/lib/gcc/../../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/i nclude" ignoring nonexistent directory "C:/mingw730/x86_64-730-posix-sjlj-rt_v5-rev0/mingw64/mingw/include" #include "..." search starts here: #include <...> search starts here: E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/include-fixed E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/include End of search list. GNU C99 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) version 7.3.0 (x86_64-w64-mingw32) compiled by GNU C version 7.3.0, GMP version 6.1.2, MPFR version 4.0.1, MPC version 1.1.0, isl version isl-0.18- GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: bf39ad0da2e83889ab66d50562b3766e COLLECT_GCC_OPTIONS='-std=c99' '-v' '-mtune=core2' '-march=nocona' E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/as.exe -v -o C:\tmp\ccRKpoBD.o C: \tmp\ccfVBdfb.s GNU assembler version 2.30 (x86_64-w64-mingw32) using BFD version (GNU Binutils) 2.30 COMPILER_PATH=E:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/;E:/mingw64/bin/../libexec/gcc/;E:/mingw64/bin/../l ib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/bin/ LIBRARY_PATH=E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/;E:/mingw64/bin/../lib/gcc/;E:/mingw64/bin/../lib/gcc/x8 6_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/lib/../lib/;E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../ ../../../lib/;E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/lib/;E:/mingw64/bin/../l ib/gcc/x86_64-w64-mingw32/7.3.0/../../../ COLLECT_GCC_OPTIONS='-std=c99' '-v' '-mtune=core2' '-march=nocona' E:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/collect2.exe -plugin E:/mingw64/bin/../libexec/gcc/x86_64-w64-mi ngw32/7.3.0/liblto_plugin-0.dll -plugin-opt=E:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/7.3.0/lto-wrapper.exe -plug in-opt=-fresolution=C:\tmp\ccNa1jcm.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt= -pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-throug h=-lmsvcrt -plugin-opt=-pass-through=-lpthread -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-liconv -plugin-opt=-p ass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmol dname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt --sysroot=C:/mingw730/x86_64-730-posix-sjlj -rt_v5-rev0/mingw64 -m i386pep -Bdynamic E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw 32/lib/../lib/crt2.o E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/crtbegin.o -LE:/mingw64/bin/../lib/gcc/x86_64-w6 4-mingw32/7.3.0 -LE:/mingw64/bin/../lib/gcc -LE:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../x86_64-w64- mingw32/lib/../lib -LE:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../../../lib -LE:/mingw64/bin/../lib/gcc/x86_ 64-w64-mingw32/7.3.0/../../../../x86_64-w64-mingw32/lib -LE:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/../../.. C: \tmp\ccRKpoBD.o -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel3 2 -liconv -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt E:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.3.0/crte nd.o COLLECT_GCC_OPTIONS='-std=c99' '-v' '-mtune=core2' '-march=nocona'
三、gcc -o
C:\Program Files\Go\src\democgo\pointerfunc\std>gcc main.c -o gcc: error: missing filename after '-o' C:\Program Files\Go\src\democgo\pointerfunc\std>gcc main.c -o main //或 gcc -o main main.c都可以 C:\Program Files\Go\src\democgo\pointerfunc\std>main i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9
完整的进行编译
只生成 main.exe
四、 gcc -c
-c,告诉gcc到汇编为止,不要进行link(链接),即-c 选项只是令 GCC 编译器将指定文件加工至汇编阶段,但不执行链接操作
gcc main.c -c
C:\Program Files\Go\src\democgo\pointerfunc\std>gcc main.c -c
C:\Program Files\Go\src\democgo\pointerfunc\std
2022-06-06 14:24 100 main.c
2022-06-06 17:20 914 main.o //
2 个文件
main.o 即目标文件
我们查看一下这一步生成的main.o文件
C:\Program Files\Go\src\democgo\pointerfunc\std>objdump -h main.o main.o: file format pe-x86-64 Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000040 0000000000000000 0000000000000000 0000012c 2**4 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 0000000000000000 0000000000000000 00000000 2**4 ALLOC, LOAD, DATA 2 .bss 00000000 0000000000000000 0000000000000000 00000000 2**4 ALLOC 3 .rdata 00000010 0000000000000000 0000000000000000 0000016c 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .xdata 0000000c 0000000000000000 0000000000000000 0000017c 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .pdata 0000000c 0000000000000000 0000000000000000 00000188 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 6 .rdata$zzz 00000040 0000000000000000 0000000000000000 00000194 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA 行: .text :代码段(存放函数的二进制机器指令) .data :数据段(存已初始化的局部/全局静态变量、未初始化的全局静态变量) .bss :bss段(声明未初始化变量所占大小) .rodata :只读数据段(存放 " " 引住的只读字符串) .comment :注释信息段 .node.GUN-stack :堆栈提示段 列: Size:段的长度 File Off :段的所在位置(即距离文件头的偏移位置) 段的属性: CONTENTS:表示该段在文件中存在 ALLOC :表示只分配了大小,但没有存内容 C:\Program Files\Go\src\democgo\pointerfunc\std>objdump -x main.o main.o: file format pe-x86-64 main.o architecture: i386:x86-64, flags 0x00000039: HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS start address 0x0000000000000000 Characteristics 0x4 line numbers stripped Time/Date Thu Jan 01 08:00:00 1970 Magic 0000 MajorLinkerVersion 0 MinorLinkerVersion 0 SizeOfCode 00000000 SizeOfInitializedData 00000000 SizeOfUninitializedData 00000000 AddressOfEntryPoint 0000000000000000 BaseOfCode 0000000000000000 ImageBase 0000000000000000 SectionAlignment 0000000000000000 FileAlignment 0000000000000000 MajorOSystemVersion 0 MinorOSystemVersion 0 MajorImageVersion 0 MinorImageVersion 0 MajorSubsystemVersion 0 MinorSubsystemVersion 0 Win32Version 00000000 SizeOfImage 00000000 SizeOfHeaders 00000000 CheckSum 00000000 Subsystem 00000000 (unspecified) DllCharacteristics 00000000 SizeOfStackReserve 0000000000000000 SizeOfStackCommit 0000000000000000 SizeOfHeapReserve 0000000000000000 SizeOfHeapCommit 0000000000000000 LoaderFlags 00000000 NumberOfRvaAndSizes 00000000 The Data Directory Entry 0 0000000000000000 00000000 Export Directory [.edata (or where ever we found it)] Entry 1 0000000000000000 00000000 Import Directory [parts of .idata] Entry 2 0000000000000000 00000000 Resource Directory [.rsrc] Entry 3 0000000000000000 00000000 Exception Directory [.pdata] Entry 4 0000000000000000 00000000 Security Directory Entry 5 0000000000000000 00000000 Base Relocation Directory [.reloc] Entry 6 0000000000000000 00000000 Debug Directory Entry 7 0000000000000000 00000000 Description Directory Entry 8 0000000000000000 00000000 Special Directory Entry 9 0000000000000000 00000000 Thread Storage Directory [.tls] Entry a 0000000000000000 00000000 Load Configuration Directory Entry b 0000000000000000 00000000 Bound Import Directory Entry c 0000000000000000 00000000 Import Address Table Directory Entry d 0000000000000000 00000000 Delay Import Directory Entry e 0000000000000000 00000000 CLR Runtime Header Entry f 0000000000000000 00000000 Reserved The Function Table (interpreted .pdata section contents) vma: BeginAddress EndAddress UnwindData 0000000000000000: 0000000000000000 000000000000003c 0000000000000000 Dump of .xdata 0000000000000000 (rva: 00000000): 0000000000000000 - 000000000000003c Version: 1, Flags: none Nbr codes: 3, Prologue size: 0x08, Frame offset: 0x0, Frame reg: rbp pc+0x08: alloc small area: rsp = rsp - 0x30 pc+0x04: FPReg: rbp = rsp + 0x0 (info = 0x0) pc+0x01: push rbp Sections: Idx Name Size VMA LMA File off Algn 0 .text 00000040 0000000000000000 0000000000000000 0000012c 2**4 CONTENTS, ALLOC, LOAD, RELOC, READONLY, CODE 1 .data 00000000 0000000000000000 0000000000000000 00000000 2**4 ALLOC, LOAD, DATA 2 .bss 00000000 0000000000000000 0000000000000000 00000000 2**4 ALLOC 3 .rdata 00000010 0000000000000000 0000000000000000 0000016c 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA 4 .xdata 0000000c 0000000000000000 0000000000000000 0000017c 2**2 CONTENTS, ALLOC, LOAD, READONLY, DATA 5 .pdata 0000000c 0000000000000000 0000000000000000 00000188 2**2 CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA 6 .rdata$zzz 00000040 0000000000000000 0000000000000000 00000194 2**4 CONTENTS, ALLOC, LOAD, READONLY, DATA SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x0000000000000000 main.c File [ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x0000000000000000 main AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0 [ 4](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .text AUX scnlen 0x3c nreloc 3 nlnno 0 [ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 8](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 10](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata AUX scnlen 0x6 nreloc 0 nlnno 0 [ 12](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .xdata AUX scnlen 0xc nreloc 0 nlnno 0 [ 14](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .pdata AUX scnlen 0xc nreloc 3 nlnno 0 [ 16](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata$zzz AUX scnlen 0x40 nreloc 0 nlnno 0 [ 18](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 __main [ 19](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 printf RELOCATION RECORDS FOR [.text]: OFFSET TYPE VALUE 0000000000000009 R_X86_64_PC32 __main 000000000000001e R_X86_64_PC32 .rdata 0000000000000023 R_X86_64_PC32 printf RELOCATION RECORDS FOR [.pdata]: OFFSET TYPE VALUE 0000000000000000 rva32 .text 0000000000000004 rva32 .text 0000000000000008 rva32 .xdata C:\Program Files\Go\src\democgo\pointerfunc\std>objdump -t main.o main.o: file format pe-x86-64 SYMBOL TABLE: [ 0](sec -2)(fl 0x00)(ty 0)(scl 103) (nx 1) 0x0000000000000000 main.c File [ 2](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 1) 0x0000000000000000 main AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0 [ 4](sec 1)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .text AUX scnlen 0x3c nreloc 3 nlnno 0 [ 6](sec 2)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .data AUX scnlen 0x0 nreloc 0 nlnno 0 [ 8](sec 3)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .bss AUX scnlen 0x0 nreloc 0 nlnno 0 [ 10](sec 4)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata AUX scnlen 0x6 nreloc 0 nlnno 0 [ 12](sec 5)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .xdata AUX scnlen 0xc nreloc 0 nlnno 0 [ 14](sec 6)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .pdata AUX scnlen 0xc nreloc 3 nlnno 0 [ 16](sec 7)(fl 0x00)(ty 0)(scl 3) (nx 1) 0x0000000000000000 .rdata$zzz AUX scnlen 0x40 nreloc 0 nlnno 0 [ 18](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 __main [ 19](sec 0)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x0000000000000000 printf //.o文件反汇编section文本代码 C:\Program Files\Go\src\democgo\pointerfunc\std>objdump -S main.o main.o: file format pe-x86-64 Disassembly of section .text: 0000000000000000 <main>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 83 ec 30 sub $0x30,%rsp 8: e8 00 00 00 00 callq d <main+0xd> d: c7 45 fc 00 00 00 00 movl $0x0,-0x4(%rbp) 14: eb 15 jmp 2b <main+0x2b> 16: 8b 45 fc mov -0x4(%rbp),%eax 19: 89 c2 mov %eax,%edx 1b: 48 8d 0d 00 00 00 00 lea 0x0(%rip),%rcx # 22 <main+0x22> 22: e8 00 00 00 00 callq 27 <main+0x27> 27: 83 45 fc 01 addl $0x1,-0x4(%rbp) 2b: 83 7d fc 09 cmpl $0x9,-0x4(%rbp) 2f: 7e e5 jle 16 <main+0x16> 31: b8 00 00 00 00 mov $0x0,%eax 36: 48 83 c4 30 add $0x30,%rsp 3a: 5d pop %rbp 3b: c3 retq 3c: 90 nop 3d: 90 nop 3e: 90 nop 3f: 90 nop
五、 gcc -E
将main.c预处理输出main.i (下面会利用组合命令,看红色部分 )
gcc -o main.i -E main.c
C:\Program Files\Go\src\democgo\pointerfunc\std>gcc -o main.i -E main.c
C:\Program Files\Go\src\democgo\pointerfunc\std
2022-06-06 14:24 100 main.c
2022-06-06 17:59 53,252 main.i
2 个文件
-E main.c 告诉编译器,要进行预编译,编译器得到指令就会对main.c执行预编译操作
-o main.i 生成目标文件 *.i
我们这里看看这样一组命令
gcc -S main.i -o main.s //这里的目标文件main.s
gcc -c main.s -o main.o //这里的目标文件main.o
gcc main.o -o main.exe //这里的目标文件是main.exe
-o 指定生成文件的文件名
六、gcc -S
将预处理输出文件main.i 汇编成 main.s文件
gcc -S main.i -o main.s
什么是目标文件?
目标文件就是源代码经过编译后但未进行链接的那些中间文件
Linux下的 .o文件就是目标文件,目标文件和可执行文件内容和
格式几乎都一样,所以我们可以广义地将目标文件和可执行文化
看成一类型文件。他们都是按照ELF文件格式存储的
windows下是按照pe格式
Linux下ELF类型的文件?
.o文件、可执行文件、核心转储文件(core dump)、.so文件(动态链
链接库)
Window下pe类型文件?
.o、.dll 等。
可执行文件的包括几个部分
File Header 、.text section 、.data section 、.bss section
文件头(File Header)
描述了整个文件的文件属性,包括目标文件是否可执行、是静态链接还 是动
态链接及入口地址、目标硬件、目标操作系统等信息、段表(描述文件中各
个段的偏移位置及属性等)
代码段(.text)
存放了程序源代码编译后生成的机器指令
数据段(.data)
存放已初始化的全局静态与非静态变量和已初始化的局部静态变量
.bss段
存放未初始化的全局变量(全局静态和非静态变量)和局部静态变量
但是.bss段只是为这些变量预留位置而已,并没有内容,所以这些变量
在.bss段中也不占据空间
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类