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

posted @ 2016-03-14 11:57  扫地猿  阅读(12046)  评论(1编辑  收藏  举报