zoukankan      html  css  js  c++  java
  • 编译选项

    参考连接:

    https://developers.redhat.com/blog/2018/03/21/compiler-and-linker-flags-gcc

    https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Option-Summary.html#Option-Summary

    https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options

    https://www.cnblogs.com/lsgxeva/p/7605141.html

    建议添加的编译选项

    -Werror:将大多数警告变成error,并中止编译;

    -Werror=all or -Wall:将尽可能多的警告显示出来;

              -Waddress   
              -Warray-bounds=1 (only with -O2)  
              -Wc++11-compat  -Wc++14-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) 
              -Wbool-compare  
              -Wduplicated-cond  
              -Wcomment  
              -Wformat   
              -Wmain (only for C/ObjC and unless -ffreestanding)  
              -Wmaybe-uninitialized 
              -Wmissing-braces (only for C/ObjC) 
              -Wnonnull  
              -Wopenmp-simd 
              -Wparentheses  
              -Wpointer-sign  
              -Wreorder   
              -Wreturn-type  
              -Wsequence-point  
              -Wsign-compare (only in C++)  
              -Wstrict-aliasing  
              -Wstrict-overflow=1  
              -Wswitch  
              -Wtautological-compare  
              -Wtrigraphs  
              -Wuninitialized  
              -Wunknown-pragmas  
              -Wunused-function  
              -Wunused-label     
              -Wunused-value     
              -Wunused-variable  
          -Wunused-but-set-variable -Wvolatile-register-var

    -Werror=extra or -Wextra:除-Wall外其它的警告;

              -Wclobbered  
              -Wempty-body  
              -Wignored-qualifiers 
              -Wmissing-field-initializers  
              -Wmissing-parameter-type (C only)  
              -Wold-style-declaration (C only)  
              -Woverride-init  
              -Wsign-compare  
              -Wtype-limits  
              -Wuninitialized  
              -Wshift-negative-value  
              -Wunused-parameter (only with -Wunused or -Wall) 
              -Wunused-but-set-parameter (only with -Wunused or -Wall)  

    -Wfatal-errors:遇到第一个错误就停止,减少查找错误时间;

    -Werror=shadow:局部变量覆盖参数、全局变量,报警告;

    -Werror=incompatible-pointer-types:指针错误类型转换报警;

    -Wformat-overflow=2:范围溢出

    -O3 or -O2

    建议不添加的编译选项

    -w:关闭警告;

    调试选项

    -v:verbose,展开显示详细的编译链接命令

    -fstack-protector-all:栈空间越界检查

    -Wno-unused-function:flag前面加“no-”,可以屏蔽 defined func but not used。同理,可以在某些想打开的flags前面加“no-”前缀,从而不开启某个选项;
      也可通过progma宏来屏蔽某些文件的检查,如下:
      
    // 关闭某个文件的编译warning选项
    #pragma GCC diagnostic ignored "-Wenum-compare"
    /**
     * Code that generates this warning
     */
    
    // 关闭某个代码段的编译warning选项
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wenum-compare"
    /**
     * Code that generates this warning
     */
    #pragma GCC diagnostic pop

    -g3: 提供更多的调试信息,比如宏定义,默认-g是level 2;

    Request debugging information and also use level to specify how much information. The default level is 2.
    
    Level 0 produces no debug information at all. Thus, -g0 negates -g.
    
    Level 1 produces minimal information, enough for making backtraces in parts of the program that you don’t plan to debug. This includes descriptions of functions and external variables, and line number tables, but no information about local variables.
    
    Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

    其他杂项

    -fexceptions 建议在C++与C混合编程时,为C代码默认开启此选项;

      启用异常处理。生成传播异常所需的额外代码。对于某些目标,这意味着 GCC 会为所有函数生成帧展开信息,这会产生显着的数据大小开销,尽管它不会影响执行。如果您不指定此选项,GCC 会默认为 C++ 等通常需要异常处理的语言启用它,并为 C 等通常不需要它的语言禁用它。但是,在编译需要与用 C++ 编写的异常处理程序正确互操作的 C 代码时,您可能需要启用此选项。如果您正在编译不使用异常处理的旧 C++ 程序,您可能还希望禁用此选项。

    -D_XOPEN_SOURCE=700 选择posix的标准

  • 相关阅读:
    程序调试的利器GDB
    Building a Android Development Environment
    手机的串号IMEI/ESN标示位置图解摩托罗拉官方教程
    Linux调试信息输出串口设备号的设置
    git忽略文件提交
    Spring @Transactional事务传播范围以及隔离级别
    自定义springbootstarter
    maven 命令将jar包安装到本地仓库
    oracle 常用命令
    Oracle 用户(user)和模式(schema)的区别
  • 原文地址:https://www.cnblogs.com/zengjianrong/p/14480427.html
Copyright © 2011-2022 走看看