3

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 )
  • No problem with scons. You will have same error if you use directly `clang++`. Check where are implemented `ThrowException` and constructor and desctructor of `MyTestClass` and be sure file containing implementation are compiled and linked with other – Garf365 Mar 09 '16 at 08:33
  • After seeing your code, it cleary miss `MyTestClass.cpp` in compilation process. Maybe, giving here your `SConstruct` can help to have correct answer – Garf365 Mar 09 '16 at 08:36
  • Try this : `program = env.Program( target="test", source=test_sources+main_sources, CXXFLAGS=flags, LIBS=libs )` – Garf365 Mar 09 '16 at 09:01
  • @Garf365 thank you, it's working now, but I have a warning: `warning: Two different environments were specified for target MyTestClass.o`. And can you make an real answer so I can accept it. – Roger Afferson Mar 09 '16 at 09:12

1 Answers1

2

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.

bdbaddog
  • 3,357
  • 2
  • 22
  • 33