Useful GCC flags for C

Several of the -f code generation options are interesting:

  • The -ftrapv function will cause the program to abort on signed integer overflow (formally "undefined behaviour" in C).

  • -fverbose-asm is useful if you're compiling with -S to examine the assembly output - it adds some informative comments.

  • -finstrument-functions adds code to call user-supplied profiling functions at every function entry and exit point.


    Here are mine:

    • -Wextra-Wall: essential.
    • -Wfloat-equal: useful because usually testing floating-point numbers for equality is bad.
    • -Wundef: warn if an uninitialized identifier is evaluated in an #if directive.
    • -Wshadow: warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.
    • -Wpointer-arith: warn if anything depends upon the size of a function or of void.
    • -Wcast-align: warn whenever a pointer is cast such that the required alignment of the target is increased. For example, warn if a char * is cast to an int * on machines where integers can only be accessed at two- or four-byte boundaries.
    • -Wstrict-prototypes: warn if a function is declared or defined without specifying the argument types.
    • -Wstrict-overflow=5: warns about cases where the compiler optimizes based on the assumption that signed overflow does not occur. (The value 5 may be too strict, see the manual page.)
    • -Wwrite-strings: give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning.
    • -Waggregate-return: warn if any functions that return structures or unions are defined or called.
    • -Wcast-qual: warn whenever a pointer is cast to remove a type qualifier from the target type*.
    • -Wswitch-default: warn whenever a switch statement does not have a default case*.
    • -Wswitch-enum: warn whenever a switch statement has an index of enumerated type and lacks acase for one or more of the named codes of that enumeration*.
    • -Wconversion: warn for implicit conversions that may alter a value*.
    • -Wunreachable-code: warn if the compiler detects that code will never be executed*.

    Those marked * sometimes give too many spurious warnings, so I use them on as-needed basis.


    -save-temps

    This leaves behind the results of the preprocessor and the assembly.

    The preprocessed source is useful for debugging macros.

    The assembly is useful for determining what optimizations went into effect. For instance, you may want to verify that GCC is doing tail call optimization on some recursive functions, as without it you can potentially overflow the stack.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值