无法使用 scons 构建我的单元测试以及程序

Can't build with scons my unit tests alongside with the program

我正在尝试使用 scons 为我的代码构建测试套件。 There is 我所有的文件都处于当前状态。在尝试 scons testscons 之后,我得到了这一堆链接器错误:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
clang++ -o test main.o simplest_test.o fixture_test.o -lcppunit
fixture_test.o: In function `FixtureTest::Test1()':
fixture_test.cpp:(.text+0xd7): undefined reference to `MyTestClass::ThrowException() const'
fixture_test.o: In function `FixtureTest::~FixtureTest()':
fixture_test.cpp:(.text._ZN11FixtureTestD2Ev[_ZN11FixtureTestD2Ev]+0x14): undefined reference to `MyTestClass::~MyTestClass()'
fixture_test.o: In function `FixtureTest::~FixtureTest()':
fixture_test.cpp:(.text._ZN11FixtureTestD0Ev[_ZN11FixtureTestD0Ev]+0x14): undefined reference to `MyTestClass::~MyTestClass()'
fixture_test.o: In function `CppUnit::ConcretTestFixtureFactory<FixtureTest>::makeFixture()':
fixture_test.cpp:(.text._ZN7CppUnit25ConcretTestFixtureFactoryI11FixtureTestE11makeFixtureEv[_ZN7CppUnit25ConcretTestFixtureFactoryI11FixtureTestE11makeFixtureEv]+0x2f): undefined reference to `MyTestClass::MyTestClass()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
scons: *** [test] Error 1
scons: building terminated because of errors.

怎么了?

更新(这里添加了SConstruct文件)

env = Environment( CXX="clang++" )

flags = [
    "-std=c++11",
    "-Wall",
    "-O3",
]

libs = [ "-lcppunit" ]

test_sources = [
    "main.cpp",
    "simplest_test.cpp",
    "fixture_test.cpp",
]

main_sources = [
    "MyTestClass.cpp",
]

env.Program( target="program", source=main_sources, CXXFLAGS=flags, LIBS=libs )
program = env.Program( target="test", source=test_sources, CXXFLAGS=flags, LIBS=libs )
test_alias = Alias( "test", [program], program[0].path )

AlwaysBuild( test_alias )

更改为以下内容,您的警告应该会消失。

#LIBS should be the lib name and not the flag so
libs=['cppunit']

# Only specify the main_source to be compiled once
# in the original both Program statements were effectively requesting the object to be built, and likely that caused your warning.
main_source_objects = env.SharedObject(main_sources)

program = env.Program( target="test", source=test_sources+main_source_objects, CXXFLAGS=flags, LIBS=libs )

这应该可以解决问题。