Search code examples
c++linker-errorssconsclang++cppunit

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


I am trying to build test suite for my code with scons. There is all my files in their current state. After trying scons test and scons I get this bunch of linker errors:

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.

What's wrong?

Update (added here SConstruct file)

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 )

Solution

  • Change to the following and your warning should go away.

    #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 )
    

    That should do the trick.