gcc/g++使用笔记

来源:互联网 发布:sql server授予权限 编辑:程序博客网 时间:2024/06/03 03:47

获取极简帮助信息

输入如下的命令语句,即可获取g++的极简帮助信息:

g++ --help

输入该命令之后,会显示大量信息,其中就有如下所示的一段信息:

                ……  -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                ……

获取简要帮助信息

g++ -v --help                   //小写字母vg++ -v --help > g++_help.txt

使用上述命令可以将信息保存到名为 g++_help.txt 的文本文件,然后使用编辑工具(如Vim)打开该文件,再通过搜索功能即可找到我们想要进一步了解的参数说明。

例如我们在很多的makefile中文件经常会见到 -I -L -l 的参数,想要理解该参数的用法,只需在 g++_help.txt 文件中搜索,然后依次浏览这些搜索结果并找出你认为合适的解释即可,我们可以得到如下的搜索结果:

                ……-I <dir>                    Add <dir> to the end of the main include path                ……-I DIR                      add DIR to search list for .include directives                ……-l LIBNAME, --library LIBNAME                              Search for library LIBNAME-L DIRECTORY, --library-path DIRECTORY                              Add DIRECTORY to library search path                 ……

获取详尽帮助信息

man g++man g++ > man_g++.txt

使用上述命令可以将信息保存到名为 man_g++.txt 的文本文件,然后再使用编辑工具(如Vim)打开该文件,再通过搜索功能即可找到我们想要进一步了解的参数说明。

例如我们在很多的makefile中文件经常会见到 -g3 -O3 的参数,想要理解该参数的用法,只需在 man_g++.txt 文件中搜索,然后依次浏览这些搜索结果并找出你认为合适的解释即可,我们可以得到如下的搜索结果:

                ……-glevel-ggdblevel-gstabslevel-gcofflevel-gxcofflevel-gvmslevel        Request debugging information and also use level to specify how        much information.  The default level is 2.        Level 0 produces no debug information at all.  Thus, -g0 negates -g.        Level 1 produces minimal information, enough for making backtraces        in parts of the program that you don't plan to debug.  This        includes descriptions of functions and external variables, but no        information about local variables and no line numbers.        Level 3 includes extra information, such as all the macro        definitions present in the program.  Some debuggers support macro        expansion when you use -g3.                ……-O3 Optimize yet more.  -O3 turns on all optimizations specified by -O2    and also turns on the -finline-functions, -funswitch-loops,    -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize,    -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options.-O0 Reduce compilation time and make debugging produce the expected    results.  This is the default.                ……

基础参数说明

首先需要准备名为 test.cpp 的cpp文件作为的说明示例。

#include <iostream>using namespace std;int main(){        cout<<"hello world\n"<<endl;        return 0;}


g++编译流程为:Preprocess -> Compile -> assemble -> link

  • -E:只执行到预处理步骤。
g++ -E test.cpp                 ①g++ -E test.cpp -o test.pre     ② 可以把预处理的详细信息给重定向输出
  • -S:只执行到编译步骤,不进行汇编和链接(即只激活预处理和编译,生成的是汇编语言文件)
g++ -S test.cppg++ -S test.cpp -o test.s       ③将汇编文件重定向
  • -c:只执行到汇编步骤(即不进行链接操作),获得对应的目标文件。
g++ -c test.cpp -o test.o
  • 不带参数:执行到链接步骤,完成所有的步骤(但是通常需要 -o 参数的协助)
g++ test.cpp                    ⑤没有重定向的情况下,默认生成名为a.out的文件g++ test.cpp -o test            ⑥将结果重定向以得到可执行文件g++ test.o -o test.xxx          ⑦通常是需要连接多个目标文件,才能得到最好的可执行文件
  • -o:将执行结果的输出内容重定向到指定文件。

    如前所述,在生成可执行文件的操作时,通常需要将结果重定向输出以得到可执行文件。

常用参数说明

  • -I:指定额外的头文件搜索路径
  • -L:指定额外的库文件搜索路径
  • -l:指定编译时需要的具体库文件
  • -glevel:指定在可执行程序中所包含的标准调试信息的多少(level表示多少的级别)
  • -Olevel:指定gcc对代码的优化级别
  • -Wall:允许发出gcc能提供的所有有用的警告
  • -werror:把所有警告转换为错误,以在警告发生时中止编译过程
  • -w:关闭所有警告,建议不要使用此项
  • -Dmacro[=val]:定义macro宏(类似C语言的#define macro)
  • -Umacro:取消macro宏的定义(类似C语言的#undef macro )
  • -v:显示详细的编译、汇编、连接命令
  • -std:确定使用的语言标准版本(如C++98、C++11等)
0 0
原创粉丝点击