Scons and Boost.Test, my test project cannot communicate with my main project objects

I would like to use Boost.Test for test development.

I asked scons to create two executables, main and test. All my main project files are in ./src/

and all my test highlighted files are in./test/

The problem is this:

  • the main object files of the project are placed in. / build / src /
  • test project object files are placed in. / build / test /

and in such a configuration, my executable tester cannot communicate, since all the main project object files (the classes I run my tests on) are not in the same directory.

Do you have an idea how I could set up my scons file so that the test executable link can use object files in ./src./

?

Below is my file main.scons

:

import os
env=Environment(CPPPATH=['/usr/local/boost/boost_1_52_0/boost/','./src/'],
                CPPDEFINES=[],
                LIBPATH=['/usr/local/boost/boost_1_52_0/boost/libs/','.'],
                LIBS=['boost_regex'],
                CXXFLAGS="-std=c++0x")
env['ENV']['TERM'] = os.environ['TERM']
env.Program('Main', Glob('src/*.cpp'))

#
testEnv = env.Clone()
testEnv['CPPPATH'].append('./test/')
testEnv['LIBS'].append('boost_unit_test_framework')
testEnv.Program('Test', Glob('test/*.cpp'))

      

+3


source to share


2 answers


While the duplicate object approach is suitable for simple projects, you may run into limitations in which your test code does not need to link to the entire object space of your main program. For example, to dump a database layer that doesn't focus on a specific unit test.

Alternatively, you can create (static) shared code libraries that you link to the main executable and your test framework.



common_sources = ['src/foo.cpp', 'src/bar.cpp'] # or use Glob and then filter
env.Library("common", common_sources)
program_sources = ['src/main.cpp']
env.Program("my_program", program_sources, LIBS=['common'])
...
testEnv['LIBPATH'] = ['.']  # or wherever you build the library
testEnv.Program("unit_test", test_sources, LIBS=['common'])

      

This also eliminates the duplicate problem main()

you mention, because only lists are program_sources

and test_sources

should contain the corresponding main subroutine file.

+1


source


I continued searching and found This post on the internet that intrigued me using scons env.Object

. This object actually contains a list of all target object files.

And with a little change, I have a scons file that does what I wanted (although now I have a duplicate main function problem, but this is a different problem):



import os

env=Environment(CPPPATH=['/usr/local/boost/boost_1_52_0/boost/','./src/'],
                CPPDEFINES=[],
                LIBPATH=['/usr/local/boost/boost_1_52_0/boost/libs/','.'],
                LIBS=['boost_regex'],
                CXXFLAGS="-std=c++0x")
env['ENV']['TERM'] = os.environ['TERM']

# here I keep track of the main project object files
mainObjectFiles = env.Object( Glob('src/*.cpp')) 

env.Program('PostgresCpp', mainObjectFiles)

#
testEnv = env.Clone()
testEnv['CPPPATH'].append('./test/')
testEnv['LIBS'].append('boost_unit_test_framework')

# here I append all needed object files
testObjectFiles =  Glob('test/*.cpp')
testedObjectFiles = Glob('src/*.cpp')
allObjectFilesExceptMain = [x for x in mainObjectFiles if x != 'src/main.o']
allObjectFilesExceptMain.append(testObjectFiles)

testEnv.Program('Test',allObjectFiles)

      

0


source







All Articles