平時用的都是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