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开发者杂谈

RocketMQ专题2:三种常用生产消费方式(顺序、广播、定时)以及顺序消费源码探究

​ 在进行常用的三种消息类型例子展示的时候,我们先来说一说RocketMQ的几个重要概念:

1731
来自专栏黑泽君的专栏

day53_BOS项目_05

定区可以将取派员、分区、客户信息关联到一起。 页面:WEB-INF/pages/base/decidedzone.jsp

584
来自专栏别先生

基于jsp+servlet图书管理系统之后台用户信息删除操作

上一篇的博客写的是修改操作,且附有源码和数据库,这篇博客写的是删除操作,附有从头至尾写的代码(详细的注释)和数据库!   此次删除操作的源码和数据库:http:...

27310
来自专栏Kirito的技术分享

Spring Boot Dubbo应用启停源码分析

Dubbo Spring Boot 工程致力于简化 Dubbo RPC 框架在Spring Boot应用场景的开发。同时也整合了 Spring Boot 特性:

1422
来自专栏禁心尽力

Web层框架对网站中所有异常的统一处理

  一个网站的异常信息作为专业的人士,是不会轻易暴露给用户的,因为那样狠不安全,显得你漏是一回事,只要还是考虑到网站的数据安全问题,下面给大家分享一下一些常见的...

2168
来自专栏java达人

使用Redis做MyBatis的二级缓存

使用Redis做MyBatis的二级缓存  通常为了减轻数据库的压力,我们会引入缓存。在Dao查询数据库之前,先去缓存中找是否有要找的数据,如果有则用缓存中的数...

3975
来自专栏增长技术

Django实践:自定义用户系统

922
来自专栏有趣的django

Django rest framework源码分析(2)----权限

添加权限 (1)API/utils文件夹下新建premission.py文件,代码如下: message是当没有权限时,提示的信息 # utils/permi...

38810
来自专栏美团技术团队

不可不说的Java“锁”事

Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率。本文旨在对锁相关源码(本文中的源码来自JDK 8)、使用场景进行举例,为...

1002
来自专栏安富莱嵌入式技术分享

【RL-TCPnet网络教程】第20章 RL-TCPnet之BSD Socket客户端

本章节为大家讲解RL-TCPnet的BSD Socket,学习本章节前,务必要优先学习第18章的Socket基础知识。有了这些基础知识之后,再搞本章节会有事半功...

962

扫码关注云+社区