Scons在使用SConscript文件时更改编译器

时间:2014-12-04 20:35:30

标签: c++ scons

我刚刚开始关注scons

我创建了一个简单的test.cpp

#include <iostream>

int main(int argc, char ** argv) {
    std::cout << "Hello World\n";
    return 0;
}

并给它一个SConstruct文件

env = Environment()
env.Program(target='Hello', source=['test.cpp'])

它完美编译

g++ -o test.o -c test.cpp
g++ -o Hello test.o

现在,我将此Sconstruct文件的内容转移到SConscript文件 并在SConstruct中输入以下内容

SConscript('SConscript', variant_dir='.build_release', duplicate=0, exports={'MODE':'release'})
SConscript('SConscript', variant_dir='.build_debug', duplicate=0, exports={'MODE':'debug'})

scons选择GCC而不是g ++

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: .build_release .build_debug
gcc -o .build_debug/Hello test.o
Undefined symbols for architecture x86_64:
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
  std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in test.o

后面是一页,还有一半错误。

如果我用g ++替换gcc并从命令行运行它再次完美运行。

任何人都可以解释为什么它已经从g ++改为gcc以及我应该做些什么?

1 个答案:

答案 0 :(得分:0)

您需要使用exports = [&#34; env&#34;]将SConstruct环境传播到SConscript。

例如,SConstruct:

env = Environment(CFLAGS = CFLAGS, CPPDEFINES = [])
conf = env.Configure()
vars = Variables()
vars.AddVariables(
        BoolVariable("verbose", "Set to show compilation lines", False),
        BoolVariable("release", "Set to make a release build", False),
        EnumVariable("flavour", "Choose a build flavor", "debug",
            allowed_values = ("release","debug"),
            ignorecase = 2),
        PathVariable("CC", "The C compiler", "clang", PathVariable.PathAccept),
        PathVariable("CXX", "The C++ compiler", "clang++", PathVariable.PathAccept),
        )
vars.Update(env)
SConscript(dirs = dirs, exports=["env", "conf"], name = "SConscript")
SConscript中的

Import("env", "conf")
env.Program(target='Hello', source=['test.cpp'])

此后,SConstruct中配置的所有设置对于SConsript也是相同的。

请参阅我的other post for bigger example

希望它有所帮助。

编辑:我看到已经在评论中回答了这个问题。有趣并感谢您的信息。