如何告诉scons使用C++ 11标准

Ste*_*and 7 c++ scons c++11

我无法找到如何告诉scons接受c ++ 11标准:

SConstruct文件:

env=Environment(CPPPATH='/usr/include/boost/',
                CPPDEFINES=[],
                LIBS=[],
                SCONS_CXX_STANDARD="c++11"
                )

env.Program('Hello', Glob('src/*.cpp'))
Run Code Online (Sandbox Code Playgroud)

cpp文件:

#include <iostream>
class A{};
int main()
{
  std::cout << "hello world!" << std::endl;
  auto test = new A; // testing auto C++11 keyword
  if( test == nullptr ){std::cout << "hey hey" << std::endl;} // testing nullptr keyword
  else{std::cout << " the pointer is not null" << std::endl;}
  return 0;
};
Run Code Online (Sandbox Code Playgroud)

调用scons时出现错误消息:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o src/hello_world.o -c -I/usr/include/boost src/hello_world.cpp
src/hello_world.cpp: In function 'int main()':
src/hello_world.cpp:13:8: error: 'test' does not name a type
src/hello_world.cpp:15:7: error: 'test' was not declared in this scope
src/hello_world.cpp:15:15: error: 'nullptr' was not declared in this scope
scons: *** [src/hello_world.o] Error 1
scons: building terminated because of errors.
Run Code Online (Sandbox Code Playgroud)

显然它不明白autonullptr

Bra*_*ady 13

我不确定SCONS_CXX_STANDARDSCons是否支持.

相反,如果您使用的是GCC 4.7或更高版本,请尝试-std=c++11按如下方式传递给编译器:

env=Environment(CPPPATH='/usr/include/boost/',
                CPPDEFINES=[],
                LIBS=[],
                CXXFLAGS="-std=c++0x"
                )
Run Code Online (Sandbox Code Playgroud)

本问题所述,您可能需要-gnu++11

  • @StephaneRolland:只有通过GCC 4.6.x才能实现 - 从GCC 4.7开始,它确实应该是`-std = c ++ 11`. (4认同)
  • 现在这个答案是不正确的,当前版本的SCons支持`SCONS_CXX_STANDARD`。 (2认同)