1
votes

I am just starting to look at scons

I created a simple test.cpp

#include <iostream>

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

and gave it an SConstruct file

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

and it compiles perfectly

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

Now, I shift the contents of this Sconstruct file to a SConscript file and enter the following into SConstruct

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

And scons selects GCC instead of 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

followed by a page and a half more of errors.

If I were to replace the gcc with g++ and run from the command line it works perfectly again.

Can anybody explain why it has changed from g++ to gcc and what I should do?

1
Probably many interacting circumstances at play, but: remove the test.o file left from before you switched over to creating a SConscript file. Or switch to using duplicate=1 - nos
YES Thanks - i did mean gcc as can be seen from the screen msg. (will edit it). And - yes removing the original test.o fixed it. If you make it an answer I will mark it accordingly. Thanks. - Dave Appleton

1 Answers

0
votes

You need to propagate the SConstruct environment to SConscript using exports=["env"]..

Eg.in 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")

in SConscript:

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

After this, all settings configured in SConstruct will be the same for SConsript as well.

Please see my other post for bigger example.

Hope it helps.

EDIT: I see that has been answered in the comments already. Interesting and thanks for the info.