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

相关文章

  • Out of bag error in Random Forest

    sklearn中的RandomForestClassifier有一个参数: oob_score : bool (default=False) Whethe...

    GavinZhou
  • 机器学习基本概念-3

    前两篇介绍了ML中的一些基本概念,还有一些很重要的概念也还没有说到,作为入门教程还是需要直观点,所以先举个最简单的例子线性回归(linear regresion...

    GavinZhou
  • ResNet && DenseNet(原理篇)

    这篇博客讲现在很流行的两种网络模型,ResNet和DenseNet,其实可以把DenseNet看做是ResNet的特例 文章地址: [1]Deep Re...

    GavinZhou
  • Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000...

    ShenduCC
  • 聊聊flink LocalEnvironment的execute方法

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/DataSet.java

    codecraft
  • 如何阅读JVM 源码

    JDK中JVM(安装在本地C:\Program Files\Java\jdk1.8.0_121\jre\bin\server下jvm.dll)本身并不开源,只能...

    王小明_HIT
  • 多目标进化算法应用于提高医药数据领域学习器的性能(CS AI)

    原文标题完整翻译:多目标进化算法应用于提高在医药数据领域使用整体特征选择和离散化模型的学习器的性能

    Donuts_choco
  • Redis 为什么这么快?(9)

    因为单线程已经够用了,CPU不是redis的瓶颈。Redis的瓶颈最有可能是机器内存或者网络带宽。既然单线程容易实现,而且CPU不会成为瓶颈,那就顺理成章地采用...

    兜兜毛毛
  • 分析树莓派3b+上电启动流程

    最近在玩树莓派,觉得这个树莓派的启动过程有点意思。所以在收集很多信息之后,个人也进行了一些实验和总结。先看一段原始资料:

    bigmagic
  • copy-and-swap idiom

    This answer is from https://stackoverflow.com/a/3279550/10133369

    努力努力再努力F

扫码关注云+社区

领取腾讯云代金券