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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM