cc1plus:“-Wno-unused-result”错误

时间:2013-10-19 10:15:55

标签: c++ cmake

我想编译项目,但我有错误:

[  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 Mountain Lion,gcc版本是(MacPorts gcc48 4.8.1_3)4.8.1

Makefile由CMake 2.8-12完成

你能帮我吗,PLZ?

1 个答案:

答案 0 :(得分:7)

您正在使用(直接或通过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中的jbar()未使用。但是,不允许优化对foo1()的呼叫,因为该呼叫也会影响x,而呼叫foo2()没有副作用。因此,为了避免警告,只需忽略未使用的结果,避免不必要的计算

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