最近整理了下代码警告问题。这里记录一下。

在以前某个项目上竟然用-w把gcc的警告给关闭了,怪不得编译代码完全没警告,多漂亮的代码!

1、未使用变量、未使用函数返回值,未使用变量:

warning: unused variable ‘ret’ [-Wunused-variable]int ret;

-->修改:删之。没有达到函数末尾:

warning: control reaches end of non-void function [-Wreturn-type] int open(const char* file) {if(foo==11){return ok;}// --> 修改:此处要加上返回值 }

没有返回值:

warning: no return statement in function returning non-void [-Wreturn-type]int Open(char* file){}

--> 修改:加上返回值2、参数一致性,参数类型检查空字符串:

warning: zero-length gnu_printf format string [-Wformat-zero-length]sprintf(buffer, "");

类型不对:

warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char*’ [-Wformat=] warning: format ‘%d’ expects argument of type ‘int*’, but argument 4 has type ‘short int*’ [-Wformat=] --> 修改:按printf格式来修改,unsigned long使用%lu。不要用%d来打印字符串指针。

有符号和无符号:

warning: pointer targets in passing argument 2 of ‘ll_foobar’ differ in signedness [-Wpointer-sign] expected ‘unsigned char *’ but argument is of type ‘char *’

有符号和无符号比较:

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

--> 修改:强制转换类型参数类型:

warning: passing NULL to non-pointer argument 2 of ‘void* memset(void*, int, size_t)’ [-Wconversion-null]memset(foobar, NULL, sizeof(foobar)); (C++中,NULL是指针,不是数值,而memset要求的是数值0)

--> 修改:用0来代替,或将NULL强转为INT。比较诡异:

warning: argument to ‘sizeof’ in ‘char* strncpy(char*, const char*, size_t)’ call is the same expression as the destination; did you mean to provide an explicit length? [-Wsizeof-pointer-memaccess]strncpy(ptr, buffer, sizeof(ptr));

用意本身是好的,用法却是错的,ptr是指针,sizeof指针只能得到4。类似的有:

warning: argument to ‘sizeof’ in ‘void* memset(void*, int, size_t)’ call is the same expression as the destination; did you mean to dereference it? [-Wsizeof-pointer-memaccess]memset(this, 0, sizeof(this));

--> 修改:strncpy、memset第三个参数按实际给数值。C++类构造函数初始化顺序问题:

warning: CFoobar::m_nInit will be initialized after [-Wreorder]int m_nInit;^ warning: ‘int CFoobar::m_nTime’ [-Wreorder]int m_nTime’;

--> 修改:按声明的顺序排列。常量字符串:

warning: deprecated conversion from string constant to ‘CHAR* {aka char*}’ [-Wwrite-strings]

--> 修改:字符串要加const。3、打印格式化

warning: too few arguments for formatwarning: too many arguments for format

如本身要打印2个参数,但只有一个%。 myprintf("hello world %d!\n", count, getname());--> 修改:认真检查参数。4、类型一致性,类型转换整数溢出:

warning: integer overflow in expression [-Woverflow] #define BIG_SIZE 800*1024*1024*1024

转换转换:

warning: narrowing conversion of ‘height’ from ‘DWORD {aka unsigned int}’ to ‘int’ inside { } is ill-formed in C++11 [-Wnarrowing]my_dst_size foo = {width, height};

my_dst_size类型为int,但width和height为DWORD--> 修改:强制转换类型,保持一致。

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]my_pos.pos = {0,0};

my_pos.pos为坐标。--> 修改:一一赋值。

my_pos.pos.x = 0; my_pos.pos.y = 0;

5、括号、优先级

warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses] #define HELLO ((((z>>51)^(y<<32 y="">>23)^(z<<5)))^((sum^y)+(k[p&7^e]^z)))

--> 添加括号

#define HELLO ((((z>>51)^(y<<32 y="">>23)^(z<<5)))^((sum^y)+(k[(p&7)^e]^z)))

另一个例子:

warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]return id == foo::Get() || foo::Get() != bar::Get() && bar::Go();

6、switch

warning: enumeration value ‘LL_FOO’ not handled in switch [-Wswitch]

-->枚举类型没有完全使用,如没有default语句。添加之7、

warning: the address of ‘filename’ will always evaluate as ‘true’ [-Waddress] #define LL_STR(foo) foo ? foo : ""

其它

error: function declaration isn’t a prototype -->函数声明如无参数,要加上void

李迟 2015.4.22周三中午

gcc较高版本的一些编译警告收集相关推荐

  1. 【gcc】高版本gcc编译出的程序在低版本glibc机器上运行

    目录 1.静态编译(多数场景不行) 2.容器发布(部分场景可以使用) 3.安装部署devtoolset 4.打包依赖的so发布(通用方案) 3.1 方式1 在编译时设置rpath和dynamic li ...

  2. linux怎么升级gcc版本号,GCC升级至高版本

    系统版本:CentOS 7.2 gcc原有版本:4.8 要升级为:6.1.0 注意:测试过升级到6.4版本,但各种报错,无耐放弃 相关场景:在做某个操作时提示"/lib64/libstdc+ ...

  3. 高版本glibc环境编译兼容低版本机器的.so文件

    https://blog.csdn.net/wangmingsuyang/article/details/80089984 https://blog.csdn.net/nullzhou/article ...

  4. Mac OS 10.14 低版本如何安装Xcode 11.6 高版本教程

    文章目录 0. 关于安装高版本XCode,点击agree后就没反应了,也没有报错的解决方案 0. 关于高版本的XCode编译工程报错, 而低版本的XCode编译正常的解决方案 1. 电脑系统版本 2. ...

  5. CentOS6安装devtoolset(使用高版本gcc)GCC 4.8 GCC 4.9 GCC 5.2

    CentOS6安装devtoolset(使用高版本gcc)GCC 4.8 GCC 4.9 GCC 5.2 Aria2要求gcc 4.8以上的版本才能编译,然而CentOS6源里的gcc版本才4.4 G ...

  6. 通过安装scl软件集,使用高版本gcc的方法

    SCL软件集(Software Collections)是为了给 RHEL/CentOS 用户提供一种以方便.安全地安装和使用应用程序和运行时环境的多个(而且可能是更新的)版本的方式,同时避免把系统搞 ...

  7. centos上使用高版本gcc、g++

    0x0 在centos7上gcc版本是4.85,在编译一些代码时需要使用g++的一些新特性,而网上大多教程都是重新编译gcc,太麻烦了,在centos 7上默认是yum search不到高版本的gcc ...

  8. linux6.7能升级6.8吗,CentOS 六、7升级gcc至4.八、4.九、5.二、6.三、7.3等高版本

    CentOS 7虽然已经出了不少年了,但依然会有不少人选择安装CentOS 6,CentOS 6有些依赖包和软件都比较老旧,现在天的主角gcc编译器,CentOS 6的gcc版本为4.4,CentOS ...

  9. CentOS 7.8使用devtoolset-9使用高版本gcc version 9.3.1

    问题原因 CentOS 7的gcc版本为4.8.5,Redis 6.0.5最低需要gcc4.9,因此需要升级gcc版本 from redis 6.0.5, building redis from so ...

最新文章

  1. FTP同步的另类解决办法——NetDrive
  2. webpack 打包
  3. [2020.11.26NOIP模拟赛]勇者的后缀【SA,RMQ,主席树,二分】
  4. 计算机网络的带宽是指网络可通过的,计算机网络及带宽概念.ppt
  5. 在 Laravel 5 中集成七牛云存储实现云存储功能
  6. python中tensor与variable_TensorFlow中Variable和get_variable之间的区别
  7. IOS开发--仿制网易新闻
  8. latex 图片缩小指定比例
  9. 【FreeSWITCH】录音功能调试报告-2
  10. Python学习笔记(15) 网络爬虫使用proxy实现自动换IP防封锁
  11. visio2010取消连接线交叉出的跨线
  12. ps怎么将png做成gif_PS里面图片是gif的怎么转换成jpg和png的?
  13. html将图片转为圆形并居中
  14. 银行账号正则_求一个能验证银行卡号的正则表达式
  15. 王煜全:AI独角兽必须进行业务升级 否则必死无疑
  16. Vue.js 入门 :去哪儿网APP案例 学习记录
  17. php中文拼音模糊,两种php中文字符转拼音问题解决方法
  18. 入行web前端开发可以做什么工作
  19. 儿童手表运动轨迹和路径追踪_如何将智能手表或健身追踪器用作静音闹钟
  20. 黄博的机器学习课程开课了!

热门文章

  1. 这个温州人,是中国开店最多的炸鸡王者
  2. 小红书回应行政处罚:因12月央视报道提及未成年信息审核漏放
  3. Facebook再次发生全球性宕机
  4. 哪吒U Pro试驾:透明A柱超实用、满电500公里保底续航
  5. 字节跳动和腾讯不正当竞争案将于深圳开庭 抖音:我们也是看新闻才知道本月24日要开庭...
  6. 中国首枚芯片邮票问世:搭载NFC芯片 可APP读取
  7. iPhone 12 Pro/Pro Max最新渲染图曝光
  8. 苏宁官方辟谣“员工猝死”:因个人身体原因晕倒
  9. 金山云纳斯达克敲钟上市,雷军手中又多了家上市公司
  10. 抖音联合巨量引擎、飞书推出五项举措 助中小企业共度疫情难关