Ubuntu gcc编译报错:format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]

平时用的都是Centos系统,今天偶然在Ubuntu下编译了一次代码,发现报错了:

源码:

#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int main(int argc,char * argv[])
{
    struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("time %u:%u\n",tv.tv_sec,tv.tv_usec);
    return 0;
}

这样几行代码,按理说不应该有错的,错误信息:

warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]
  printf("time %u:%u\n",tv.tv_sec,tv.tv_usec);warning: format ‘%u’ expects argument of type ‘unsigned int’, but argument 3 has type ‘__suseconds_t’ [-Wformat=]

开始的时候没有注意到错误信息最后的[-Wformat=]提醒,一直以为是类型匹配错了,把%u改成了%llu仍旧是不行。最后才注意到提醒。

然后在Ubuntu官网找到了原因:

NOTE: In Ubuntu 8.10 and later versions this option is enabled by default for C, C++, ObjC, ObjC++.  To disable, use -Wformat=0.

然后在编译的时候改成了:gcc test.c -Wformat=0 就没问题了。Wformat这个配置在Centos下默认是关闭的,所以一直没报错,如果编译的时候打开,也会提示一样的错误。

看来不同的平台,很多默认的设置还是不一样的。

Ubuntu官网的解释:http://manpages.ubuntu.com/manpages/oneiric/en/man1/gcc.1.html
原文链接: https://www.cnblogs.com/likui360/p/5275203.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/230211

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午2:35
下一篇 2023年2月13日 下午2:36

相关推荐