(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
参考:https://man7.org/linux/man-pages/man1/g++.1.html
参考:https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Option-Summary.html#Option-Summary
参考:https://blog.csdn.net/ivan240/article/details/5363395
参考:https://blog.csdn.net/sinat_36219858/article/details/80348889
引言
下面对gcc和g++的常用项进行一些罗列,方便需要时快速查找使用。
gcc命令:
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-Wpedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [@file] infile...
g++和gcc大多选型都相同。
帮助与版本:
–help 显示gcc帮助信息 Display this information
–version 显示gcc版本 Display compiler version information
–help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,…]
显示特定类型的参数选项信息 Display specific types of command line options
(Use ‘-v --help’ to display command line options of sub-processes)
编译文件选项:-c -S -E -o file -g
-g 带调试信息,gdb下可以看到源码 Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.
-c 编译和汇编,但不链接,通常用于输出.o文件 Compile and assemble, but do not link
-S 仅编译,不汇编和链接,通常用于输出汇编.asm文件 Compile only; do not assemble or link
-E 仅预处理,不编译、汇编和链接,通常用于查看预编译后的文件内容 Preprocess only; do not compile, assemble or link
-o 指定输出文件 Place the output into
-v 启用冗余输出 Enable verbose output
-v 打印编译使用命令出来 Print (on standard error output) the commands executed to run the stages of compilation.
-D macro[=defn] 定义宏信息 Predefine name as a macro, default with definition 1.
-U name 取消宏定义信息 Cancel any previous definition of name, either built in or provided with a -D option.
-l library 链接时查找引用该库文件 Search the library named library when linking.
-I DIRECTORY 添加头文件查找路径 Add DIRECTORY to header file search path
-L DIRECTORY, --library-path DIRECTORY 添加库查找路径 Add DIRECTORY to library search path
-B 添加编译查找路径 Add to the compiler’s search paths
-std= 指定源码使用的gcc库版本,例如-std=c++11指定使用c++11语法和库 Assume that the input sources are for
-pipe 使用管道替代中间文件产生,该种方式可以减少中间文件产生 Use pipes rather than intermediate files
编译优化参数:-O -O0 -O1 -O2 -O3 -Os -Ofast -Og
-O 设置编译优化级别,常见的-O0(不启用优化),-O2/-O3(全局优化) Set optimization level to
-Os 启用优化:空间占用优先于速度 Optimize for space rather than speed
-Ofast 启用优化:速度优先,忽略规则遵从性 Optimize for speed disregarding exact standards compliance
-Og 启用优化:不影响调试的优化 Optimize for debugging experience rather than speed or size
-Werror 把所有告警作为错误 Treat all warnings as errors
-Werror= 指定特定告警作为错误 Treat specified warning as error
-Wshadow 当local变量遮盖住另一个时告警 Warn when one local variable shadows another
-Wunused-variable 变量未使用时告警 Warn when a variable is unused
-Wno-attributes 关闭属性告警
-w 不输出告警 Suppress warnings
编译库参数:-shared -fpic
-shared 创建共享库,该种类型库可以不用链接其它库,使用该库时再连接 Create a shared library
-fPIC 尽可能创建位置无关代码 Generate position-independent code if possible (small mode)
另外一些参数:
-pie 创建一个位置无关的执行文件,让程序能装载在随机,放之缓冲区溢出攻击等。 Create a position independent executable
-print-search-dirs 打印编译查找路径 Display the directories in the compiler’s search path
-fPIE Generate position-independent code for executables if possible (small mode)
例子:sqlite编译
来看一看sqlite的编译选项:
gcc -c sqlite3.c -fPIC -lpthread -ldl -o sqlite3.o
ar -r ./lib/libsqlite3.a sqlite3.o
1- 首先编译sqlite3.c生成sqlite3.o
2- 然后使用ar把sqlite3.o打包到静态库包libsqlite3.a
附录:g++的help显示
$ g++ --help
Usage: g++ [options] file...
Options:
-pass-exit-codes Exit with highest error code from a phase
--help Display this information
--target-help Display target specific command line options
--help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
Display specific types of command line options
(Use '-v --help' to display command line options of sub-processes)
--version Display compiler version information
-dumpspecs Display all of the built in spec strings
-dumpversion Display the version of the compiler
-dumpmachine Display the compiler's target processor
-print-search-dirs Display the directories in the compiler's search path
-print-libgcc-file-name Display the name of the compiler's companion library
-print-file-name=<lib> Display the full path to library <lib>
-print-prog-name=<prog> Display the full path to compiler component <prog>
-print-multiarch Display the target's normalized GNU triplet, used as
a component in the library path
-print-multi-directory Display the root directory for versions of libgcc
-print-multi-lib Display the mapping between command line options and
multiple library search directories
-print-multi-os-directory Display the relative path to OS libraries
-print-sysroot Display the target libraries directory
-print-sysroot-headers-suffix Display the sysroot suffix used to find headers
-Wa,<options> Pass comma-separated <options> on to the assembler
-Wp,<options> Pass comma-separated <options> on to the preprocessor
-Wl,<options> Pass comma-separated <options> on to the linker
-Xassembler <arg> Pass <arg> on to the assembler
-Xpreprocessor <arg> Pass <arg> on to the preprocessor
-Xlinker <arg> Pass <arg> on to the linker
-save-temps Do not delete intermediate files
-save-temps=<arg> Do not delete intermediate files
-no-canonical-prefixes Do not canonicalize paths when building relative
prefixes to other gcc components
-pipe Use pipes rather than intermediate files
-time Time the execution of each subprocess
-specs=<file> Override built-in specs with the contents of <file>
-std=<standard> Assume that the input sources are for <standard>
--sysroot=<directory> Use <directory> as the root directory for headers
and libraries
-B <directory> Add <directory> to the compiler's search paths
-v Display the programs invoked by the compiler
-### Like -v but options quoted and commands not executed
-E Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
-c Compile and assemble, but do not link
-o <file> Place the output into <file>
-pie Create a position independent executable
-shared Create a shared library
-x <language> Specify the language of the following input files
Permissible languages include: c c++ assembler none
'none' means revert to the default behavior of
guessing the language based on the file's extension
Options starting with -g, -f, -m, -O, -W, or --param are automatically
passed on to the various sub-processes invoked by g++. In order to pass
other options on to these processes the -W<letter> options must be used.
For bug reporting instructions, please see:
<http://bugzilla.redhat.com/bugzilla>.
(Owed by: 春夜喜雨
http://blog.csdn.net/chunyexiyu)