前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gcc命令

gcc命令

作者头像
GavinZhou
发布2018-01-02 15:48:47
1.1K0
发布2018-01-02 15:48:47
举报
文章被收录于专栏:机器学习实践二三事

Linux底下搞开发,不可避免的要使用到gccgcc选项众多,下面记录下常见的一些选项,网上好多博客也说这个但是很多的都是不对的,我的博客记录参见man gcc,应该还是比较准的

常用的选项:

gcc [-c|-S|-E] [-std=standard] [-g] [-pg] [-Olevel] [-Wwarn…] [-Wpedantic] [-Idir…] [-Ldir…] [-Dmacro[=defn]…] [-Umacro] [-foption…] [-mmachine-option…] [-o outfile] [@file] infile…

功能综述:

-E 预处理

-S 汇编

-c 只编译不链接

-o 输出文件

-D 指定宏

-Ixxx 指定头文件搜索目录xxx(没有空格)

-Wall 打开警告信息

-O 优化

-g 调试信息

-std 指定语言标准

-Ldir 把dir加到库文件的搜索路径中,而且gcc会在搜索标准库文件之前先搜索dir(没有空格)

-lxxx 在连接的时候搜索xxx动态库,越底层的库越要放在后面(没有空格)

-fPIC 产生与位置无关的代码

-Wall(打开所有警告信息)

-Wall turns on the following warning flags:

-Waddress -Warray-bounds (only with -O2) -Wc++11-compat

-Wchar-subscripts -Wenum-compare (in C/ObjC; this is on by default in C++) -Wimplicit-int (C and Objective-C only)

-Wimplicit-function-declaration (C and Objective-C only) -Wcomment

-Wformat -Wmain (only for C/ObjC and unless -ffreestanding)

-Wmaybe-uninitialized -Wmissing-braces (only for C/ObjC) -Wnonnull

-Wparentheses -Wpointer-sign -Wreorder -Wreturn-type

-Wsequence-point -Wsign-compare (only in C++) -Wstrict-aliasing

-Wstrict-overflow=1 -Wswitch -Wtrigraphs -Wuninitialized

-Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value

-Wunused-variable -Wvolatile-register-var.

-g(产生调试信息)

-g Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.

使用了-g选项,生成的app体积会变大,其中包含相应的调试信息,可以使用gdblist选项进行查看

-O(优化)

-O(大写)有几个优化级别0-3,数字越大优化的级别越高,意味着你的程序相对高效快速

-O1 Optimize. Optimizing compilation takes somewhat more time, and a lot more memory for a large function.

-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O, this option increases both compilation time and the performance of the generated code.

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

还有其它的几个选项,比如-Os-Og-Ofast,相关的使用能够说明大家自己man看一下就明白了,这里我直说常用的选项

-D(定义宏)

就相当与你用#define定义的宏一样,比如定义宏DEBUG,这样写-DDEBUG,注意没有空格哈

-D name Predefine name as a macro, with definition 1.

-D name=definition The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive. In particular, the definition will be truncated by embedded newline characters.

-I(包含头文件的目录)

比如,你自定义了一个头文件dict.h其放置在工程根目录下的include子目录下,源文件在src下,其不在一个目录,编译时需要指定头文件存放的目录,如下:

gcc -Wall -I../include main.c dict.c -o app

这里的话,好多网上的博客都说错了,-I之后是没有空格的,这点千万要注意

Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated . If dir begins with “=”, then the “=” will be replaced by the sysroot prefix; see –sysroot and -isysroot.

-o(输出)

输出结果,不写的话系统默认名为a.out

Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

比如: 将hello.c文件编译生成的结果命名为helloApp,可以这样

gcc hello.c -o helloApp

-E(预处理)

将原始的c文件的#include包含的头文件展开,一般生成.i文件

Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. Input files that don’t require preprocessing are ignored.

比如:

gcc -E hello.c -o hello.i

-S(汇编)

生成汇编文件,一般输出为.s文件

Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. By default, the assembler file name for a source file is made by replacing the suffix .c, .i, etc., with .s. Input files that don’t require compilation are ignored.

比如:

gcc -S hello.c -o hello.s

-c(只编译不链接)

一般生成object文件,后缀为.o

Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. Unrecognized input files, not requiring compilation or assembly, are ignored.

比如:

gcc -c hello.c

-l(链接动态连接库so) -Ldir

搜索动态连接库,使用-L-l

-L Add directory dir to the list of directories to be searched for -l.

-l 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.)

-L在指定路径后并没有指定目标文件,所以要用到-l 参数指定文件

比如:

gcc -o hello hello.c -I../include -L/home/hello/lib -lworld

表示: /home/hello/lib作为第一个寻找库文件的目,寻找libworld.soso文件,头文件优先在include文件夹下搜索

-static(强制使用静态链接库)

使用静态链接库(.a),不适用动态的so文件

On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档