GCC 编译 warnings

来源:互联网 发布:飞鸟淘宝客赚钱吗 编辑:程序博客网 时间:2024/05/29 02:13

1. 编译选项-g:Generate debug information when compiling.

2. 优化选项-O0:关闭所有优化选项。

3. Warning:(GCC)

1) Warning: multi-character character constant

Could be suppressed by -Wno-multichar

There're three kinds of character constants: Normal character constants, Multicharacter constants and Wide-character constants.

    char ch = 'a';    int mbch = '1234';    wchar_t wcch = L'ab';


Mbch is of type int(signed), has 4 meaningful characters.

Gcc compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character.

'ab' for a target with an 8-bit char would be interpreted as:

(int) ((unsigned char) 'a' * 256 + (unsigned char) 'b') = 97*256+98

'1',x          0x31
'12',x        0x3132
'123',x      0x313233
'1234',x    0x31323334

判断系统是big endian还是little endian的方法:

    if (('1234' >> 24) == '1')    {        //Little endian    }    else if (('4321' >> 24) == '1')    {        //Big endian    }


2) Warning: operation on xx may be undefined

序列点问题。为什么 a[i] = i++; 不能正常工作?子表达式 i++ 有一个副作用,它会改变 i 的值。由于 i 在同一表达式的其它地方被引用,这会导致无定义的结果,无从判断该引用(左边的 a[i] 中)是旧值还是新值。(尽管 在 K&R 中建议这类表达式的行为不确定,但 C 标准却强烈声明它是无定义的),具体实现取决于编译器。

3) Warning: conversion to xxx from yyy may alter its value

Gcc promotes unsigned char/uint16_t/uint8_t  to type int for for all arithmetic. Need to apply static_cast<>.

4) Warning: function might be possible candidate for attribute 'noreturn'

打开了 -Wmissing-noreturn。A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. With noreturn attribute it can then optimize without regard to what would happen if function ever did return. This makes slightly better code. More importantly, it helps avoid spurious warnings of uninitialized variables. The noreturn keyword does not affect the exceptional path. 给函数加上 __attribute__((noreturn)) 即可消除这个warning。

5)Warning: deprecated conversion from string constant to "char *"

void SomeFunc (char* str){}int _tmain(int argc, _TCHAR* argv[]){    SomeFunc("Hello!");    return 0;}


SomeFunc() 的输入时char*,含义是:给我个字符串,我要修改它。而传给它的字面常量是没办法修改的,将char* 改成 const char*,消除这个warning.

 

6) Warning: implicit declaration of function 'malloc'/'free', incompatible implicit declaration of built-in function 'malloc'/'free'

要显示的#include <stdlib.h>

 7) Warning: Dereferencing type-punned pointer will break strict-aliasing rules

打开了-fstrict-aliasing and -Wstrict-aliasing. Suppress with -fno-strict-aliasing

Strict-aliasing rule: An object of one type is assumed never to reside at the same address as an object of a different type, unless the types are almost the same. 编译器希望不同类型的对象不会指向同一个地址。

8) Warning: inlining failed in call to xxx: call is unlikely and code size would grow

打开了-Winline: Warn if a function can not be inlined and it was declared as inline. Even with this option, the compiler will not warn about failures to inline functions declared in system headers.

相关选项:

-fno-inline: Don't compile statement functions inline. Might reduce the size of a program unit--which might be at expense of some speed (though it should compile faster). Note that if you are not optimizing, no functions can be expanded inline.

-finline-functions: Interprocedural optimizations occur. However, if you specify -O0, the default is OFF. Enables function inlining for single file compilation.

 

9) Warning: cannot optimize loop, the loop counter may overflow

打开了-Wunsafe-loop-optimizations: Warn if the loop cannot be optimized because the compiler could not assume anything on the bounds of the loop indices. With -funsafe-loop-optimizations warn if the compiler made such assumptions.

相关选项:

-funsafe-loop-optimizations: Enable unsafe loop optimizations, e.g. assume loop indices never overflow, etc

 

 

原创粉丝点击