cc1plus:"-Wno-unused-result"错误

use*_*535 1 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
Run Code Online (Sandbox Code Playgroud)

OSX Mountain Lion,gcc版本是(MacPorts gcc48 4.8.1_3)4.8.1

Makefile由CMake 2.8-12完成

你能帮我吗,PLZ?

Wal*_*ter 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
}
Run Code Online (Sandbox Code Playgroud)

在这里,ijbar()不使用.但是,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
}
Run Code Online (Sandbox Code Playgroud)

  • @EdHeal 好吧,每个警告在某些情况下都有其用途*,但并非在所有情况下。比如说,我正在使用最新的标准在 C++ 中开发代码,那么与旧标准的向后兼容性无关紧要,相应的警告充其量也很烦人。 (2认同)