Question

I have a C++ application which I'm trying to build with scons which consists of several subprograms.

Each subprogram has its own source files in a subdirectory of the source directory. These source files, e.g. source/prog1/prog1.cpp, are compiled into object files which reside in the object directory, e.e. object/prog1/prog1.o.

This works fine since each source directory has its target directory, and there's no possibility of clashes.

However, what I'm trying to do is link these object files into executables, which would be in the same bin directory. So multiple source files (object/prog1, object/prog2, etc) would all go into the same target directory (bin).

The directory layout looks like this:

application
    source
        prog1
            prog1.cpp
            something.cpp
        prog2
            prog2.cpp
            somethingelse.cpp
    object
        prog1
            prog1.o
            something.o
        prog2
            prog2.o
            somethingelse.o
    bin
        ??? <- what I'm concerned with

I'm trying to achieve that with the following SConstruct script:

env = Environment()
Export('env')

#common objects
common=env.SConscript("source/common/SConscript_object", variant_dir="object/common", duplicate=0)
Export('common')

#sub-programs
env.SConscript("source/prog1/SConscript_bin", variant_dir="bin", duplicate=0)
env.SConscript("source/prog2/SConscript_bin", variant_dir="bin", duplicate=0)

However, scons is complaining with the following error:

scons: *** 'bin' already has a source directory: 'source/prog1'.

The error goes away if I make it so that each subprogram has its own directory in the bin directory, e.g. variant_dir="bin/prog1".

So, my question is this: how can I link object files from multiple sources into the same variant dir?

Was it helpful?

Solution

In your case I would let SCons build the different binaries in their respective folder, and then use the Install builder to copy the binary files to the bin/ directory.

You would get something like:

env = Environment()
Export('env')

common = env.SConscript("source/common/SConscript_object", variant_dir="object/common", duplicate=0)
Export('common')

prog1 = env.SConscript("source/prog1/SConscript_bin", variant_dir="object/prog1", duplicate=0)
prog2 = env.SConscript("source/prog2/SConscript_bin", variant_dir="object/prog2", duplicate=0)

env.Install('bin', prog1)
env.Install('bin', prog2)

With the SConscript of the subprograms being something like

Import('env')
Import('common')

prog1 = env.Program('prog1', [ env.Glob(*.cpp), common ])

Return('prog1')

I think SCons refuses to build different targets into a unique variant directory because variants are designed to build a given target with different build settings, like debug and release mode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top