C/C++编译参数选项(GCC Command Options)笔记贴

(C/C++)使用gcc/g++命令编译选项(GCC Command Options)

-g -Wall -O2 -O3 -Werror

看到make或cmake文件中有这些乱七八糟的编译选项,有多少人是选择性忽略。
想知道怎么什么意思,google/baidu搜索也能搜到,但是总是记不住,主要是没有系统的学习过。

文章目录

  • (C/C++)使用gcc/g++命令编译选项(GCC Command Options)
    • 官方文档
      • 怎么查
    • 常用的编译选项
      • -g
      • -Werror
      • -ffunction-sections -fdata-sections

官方文档

想学习当然官方文档

https://gcc.gnu.org/onlinedocs/

怎么查

  1. 找到对应版本的手册,比如:GCC 5.5 Manual => https://gcc.gnu.org/onlinedocs/gcc-5.5.0/gcc/
  2. 打开Option Index,比如:Option Index => https://gcc.gnu.org/onlinedocs/gcc-5.5.0/gcc/Option-Index.html
  3. 搜索你要查询的编译选项,有首字母索引,也可以使用ctrl+f,注意不要加前面的-
  4. 找到后打开对应的编译选项分组中,比如Debugging Options =>https://gcc.gnu.org/onlinedocs/gcc-5.5.0/gcc/Debugging-Options.html

常用的编译选项

逐渐汇总中

-g

开启gdb调试

-g

Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.

On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but probably makes other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values are already at hand; some statements may execute in different places because they have been moved out of loops.

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

The following options are useful when GCC is generated with the capability for more than one debugging format.

-Werror

-Werror
Make all warnings into errors.

-ffunction-sections -fdata-sections

-ffunction-sections
-fdata-sections
Place each function or data item into its own section in the output file if the target supports arbitrary sections. The name of the function or the name of the data item determines the section’s name in the output file.

Use these options on systems where the linker can perform optimizations to improve locality of reference in the instruction space. Most systems using the ELF object format and SPARC processors running Solaris 2 have linkers with such optimizations. AIX may have these optimizations in the future.

Only use these options when there are significant benefits from doing so. When you specify these options, the assembler and linker create larger object and executable files and are also slower. You cannot use gprof on all systems if you specify this option, and you may have problems with debugging if you specify both this option and -g.

你可能感兴趣的:(C/C++)