gcc 常用命令行及解释

gcc - GNU project C and C++ compiler

 
gcc [option] file...

           preprocessing         compilation               assembly       linking
.c(with macros)--->.c(without macros)--->assembler input file--->object file--->executable file
 
-E, -S, -c 告诉在编译哪个阶段停止。
              -E 在执行 preprocessing 后停止,产生标准输出。
              -S 在执行 compilation 后停止,产生 .s 文件。
              -c 在执行 assembly 后停止,产生 .o 文件。
 
-std 指定编译器使用的标准。常用标准:c90, c89(就是c90), c99, gnu90(default), gnu99, c++98, gnu++98。示例:-std=c90。
              -ansi for C code 等价 -std=c90。-ansi for C++ code 等价 -std=c++98。
 
-pedantic Issue all the warnings demanded by strict ISO C and ISO C++;
               reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++.
               需配合 -std 或 -ansi 使用。
 
-g 添加标准调试信息。另一个选项 -ggdb,添加对 gdb 更友好的调试信息。
 
-pg 当想使用性能分析工具 gprof 时,需要添加该选项。
 
-O 优化(Optimize)。常用可选值:-O0(default), -O1(等价 -O), -O2(一般使用的优化级别), -O3, -Os(针对空间优化)。
 
-Wall 开启所有提醒。-Werror 把提醒当作错误。
 
-I 添加 include 目录。
-L 添加 lib 目录。
-l 将库文件添加到链接过程中,默认是链接 shared libraries。下面是为什么把 -l 放到命令行最后的原因。
           It makes a difference where in the command you write this option;
           the linker searches and processes libraries and object files in the
           order they are specified.  Thus, foo.o -lz bar.o searches library z
           after file foo.o but before bar.o.  If bar.o refers to functions in
           z, those functions may not be loaded.
-static 强制使用 static linking 来链接库(gcc 默认使用 shared linking, 不成功再试用 static linking)。链接库时有两种选择:static linking(*.a, archive file), dynamic linking(*.so, shared object)。
 
-shared
           Produce a shared object which can then be linked with other objects
           to form an executable.
 
-Dmacro[=defn] 定义 macro。
-U 取消 macro 的定义。该 macro 可以是 either built in or provided with a -D option。
试验 https://gist.github.com/4277550, -D #define, -U #undef
 
-o 定义 output 文件名。
 
-f Options of the form -fflag specify machine-independent flags.应该不常用吧。
      -fPIC emit position-independent code, suitable for dynamic linking and avoiding any limit on the
           size of the global offset table. 
-m   Each target machine types can have its own special options, starting

       with -m, to choose among various hardware models or
       configurations---for example, 68010 vs 68020, floating coprocessor or
       none.  A single installed version of the compiler can compile for any
       model or configuration, according to the options specified.应该不常用吧。

 
将 option 传给链接器:
gcc -Wl,a,b -> ld a b
-Wl,a,b=-Wl,a -Wl,b
 
man gcc  开头一段指明的常用的选项,不错。但整个文档的体积太大了。确实没法看完。
http://www.antoarts.com/the-most-useful-gcc-options-and-extensions/ 对如何学习 gcc 有介绍,对常见选项有介绍,其中运行时检测没试出来

原文链接: https://www.cnblogs.com/uhziel/p/3164048.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    gcc 常用命令行及解释

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/94055

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:26
下一篇 2023年2月10日 上午2:27

相关推荐