2013-10-19 106 views
0

我要編制項目,但我有錯誤:cc1plus: 「-Wno未使用的,結果」 錯誤

[ 9%] Building CXX object CMakeFiles/task2.dir/main.cpp.o 
cc1plus: error: unrecognized command line option "-Wno-unused-result" 
make[2]: *** [CMakeFiles/task2.dir/main.cpp.o] Error 1 
make[1]: *** [CMakeFiles/task2.dir/all] Error 2 
make: *** [all] Error 2 

OSX山獅,gcc版本是(MacPorts的gcc48 4.8.1_3)4.8.1

Makefile完成CMake 2.8-12

你能幫我嗎?

回答

5

您正在使用(直接或通過makefile)命令行選項-Wno-unused-result與(我假設)gcc編譯器。但是,gcc並不認可這個選項,我認爲這個選項旨在抑制關於不使用計算結果的警告。有了gcc,你應該使用選項-Wno-unused-value

但是,請注意,(像幾乎所有警告)這是一個有用的警告,這不應該被壓制或忽略。如果不使用計算結果,則整個計算可能是多餘的,可能會被忽略而無效。事實上,編譯器可能會優化它,如果它可以肯定它沒有副作用,。例如

int foo1(double&x) // pass by reference: we can modify caller's argument 
{ 
    int r=0; 
    // do something to x and r 
    return r; 
} 

int foo2(double x) // pass by value: make a local copy of argument at caller 
{ 
    return foo1(x); // only modifies local variable x 
} 

void bar(double&x) 
{ 
    int i=foo1(x);  // modifies x 
    int j=foo2(x);  // does not modify x 
    // more code, not using i or j 
} 

這裏,i和在bar()j不使用。但是,優化foo1()以外的呼叫是不允許的,因爲該呼叫也影響x,而呼叫foo2()沒有副作用。因此,爲了避免該警告,只需忽略未使用的結果,並避免 unneccary compuations

void bar(double&x) 
{ 
    foo1(x); // ignoring any value returned by foo1() 
      // foo2(x) did not affect x, so can be removed 
    // more code 
} 
+0

哦,它編譯!非常感謝,但還有一個問題:我不能更改cmakelists,因爲此項目將在另一臺筆記本電腦上進行檢查。 – user2897535

+0

@ user2897535這是另一個問題的另一個問題。如果此答案解決了您的初始問題,請接受它(綠色勾號)。 – Walter

+0

請啓用所有警告 –