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 条评论
登录 后参与评论

相关文章

来自专栏比原链

剥开比原看代码15:比原是如何转帐的

Gitee地址:https://gitee.com/BytomBlockchain/bytom

652
来自专栏安恒网络空间安全讲武堂

护网杯easy laravel ——Web菜鸡的详细复盘学习

复现让我发现了很多读wp以为懂了动手做的时候却想不通的漏掉的知识点(还是太菜orz),也让我对这道题解题逻辑更加理解。所以不要怂,就是干23333!

823
来自专栏性能与架构

Redis Stream 实践

stream 是一个日志形式的存储结构,可以往里追加数据,每条数据都会生成一个时间戳ID,stream 也有便捷的读取数据的模型。

1482
来自专栏葡萄城控件技术团队

如何在 ASP.NET MVC 中集成 AngularJS(2)

在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩、应用程序版本自动刷新和工程构建等内容。 下...

22110
来自专栏WeTest质量开放平台团队的专栏

了解和分析iOS Crash

原文链接:http://wetest.qq.com/lab/view/404.html

1292
来自专栏比原链

Derek解读Bytom源码-启动与停止

Gitee地址:https://gitee.com/BytomBlockchain/bytom

893
来自专栏DeveWork

wp_nav_menu 函数经Walker_Nav_Menu 类自定义导航菜单HTML

WordPress 中有一个导航菜单函数wp_nav_menu,通过这个函数可以控制菜单输出的HTML 结构,一般开发者用这个函数的一般是在输出HTML 中自定...

2607
来自专栏老码农专栏

原 荐 RESTFul 服务测试自动化的艺术

1373
来自专栏游戏杂谈

关于seajs

虽然已经有很长时间没写JavaScript,但很多时候看到一些应用还是会带着好奇心去研究一下。之前是看腾讯的朋友网,它的webchat做的很不错(虽然ff下有b...

883
来自专栏ppjun专栏

【详细】Android热更新Bugly集成配置

上一篇文章说道tinker的热更新,可是少了点补丁包的管理,这一篇文章介绍的bugly就是增强版的,更加方便你集成tinker和包括了补丁包的后台管理。 为什...

772

扫码关注云+社区