gcc命令

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.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

812
来自专栏用户2442861的专栏

Qt学习之路_6(Qt局域网聊天软件)

http://www.cnblogs.com/tornadomeet/archive/2012/07/04/2576355.html

562
来自专栏码匠的流水账

使用webflux提升数据导出效率

两种方法目前看来用时差不多,不过后者可以避免超时。当然使用传统mvc也可以实现类似效果,就是拿到response的输出流不断地write和flush。不过web...

722
来自专栏Android干货

Android项目实战(八):列表右侧边栏拼音展示效果

2745
来自专栏转载gongluck的CSDN博客

cocos2dx 2048

#include "GameScene.h" #include "CardSprite.h" #include "ScoreBoard.h" #include ...

3388
来自专栏转载gongluck的CSDN博客

自己在Qt上做的辣鸡计算器

#include "widget.h" #include "ui_widget.h" #include <QPushButton> #include <QDeb...

4455
来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

5705
来自专栏c#开发者

自定义ios keyboard tool bar代码片段

-(UIToolbar *)createActionBar { UIToolbar *actionBar = [[UIToolbar alloc] in...

2436
来自专栏吴伟祥

SSM框架整合 原

532
来自专栏落影的专栏

iOS开发-OpenGL ES画图应用思考题

这是一篇OpenGL ES的实战,紧接 入门教程3 学了OpenGL ES一段时间,用这个应用来练练手。 OpenGL ES系列教程在这里。 OpenGL ...

3187

扫码关注云+社区