http://blog.sina.com.cn/s/blog_79d599dc0100r2vz.html
昨天一同事把代碼準備重新全新布置到新的環境上去的時候,代碼報錯了,先開始報錯如下:
error: ‘memset’ was not declared in this scope
error: ‘strcat’ was not declared in this scope
首先想到的肯定是頭文件未包含進來。但不可能,因為這套代碼已經在其它環境上編譯過N次啦,不可能代碼里面沒有添加對頭文件的包含啊。
于是想到是否是因為新環境里面include目錄下沒有對應的頭文件呢?還或是include目錄在這個環境下變了呢?不是/usr/include下面啦。
經過一查,上面兩個都不是。
這個時候就有點郁悶啦。把環境按以前的都設置一遍,還是一樣。沒辦法啦。只好放今天來重新查查啦。
今天早上來,可能時間也比較充足,所以慢慢來。既然報這個錯誤,那我把這個頭文件再添加到.h代碼里面呢?
然后發現添加一個,某個錯誤就能編譯過去。看來還真是頭文件的問題。
但接下來還有其他幾個警告,如下:
test.cpp:213: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
test.cpp: In member function ‘int test::load(std::string&)’:
test.cpp:58: warning: deprecated conversion from string constant to ‘char*’
test.cpp:58: warning: deprecated conversion from string constant to ‘char*’
test.cpp:58: warning: deprecated conversion from string constant to ‘char*’
test.cpp:58: warning: deprecated conversion from string constant to ‘char*’
test.cpp:58: warning: deprecated conversion from string constant to ‘char*’
test.cpp:94: warning: suggest explicit braces to avoid ambiguous ‘else’
test.cpp:61: warning: ignoring return value of ‘int pipe(int*)’, declared with attribute warn_unused_result
居然這些還報,一一為這些沒有判斷函數返回值的作了修改。編譯通過。
然后就想為什么這個系統要求這么嚴格呢?查一下GCC的版本,如下:
以前舊環境:
gcc 版本 4.1.2 20070626 (Red Hat 4.1.2-14)
現在的新環境:
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
原來是GCC版本不同。所以想GCC新版本對代碼的這些要求現在越來越嚴格啦。還有函數的一些檢查項,還有編譯時候MAKEFILE里面對庫文件的引用。
具體是否是因為這個嚴格的原因,沒有去詳細查GCC的文檔。但猜想是這樣吧。
以后如果代碼里面對某個函數的頭文件還是盡量包含進來吧,至少不是什么壞事。