问题
I'm thinking about using SCons for a new project. It looks really good, though I'm finding VariantDir
quite confusing.
I have a simple project with a handful of C source files in one directory, and I want to build in "normal" and in "profile" mode -- with two different sets of options to gcc. I want the outputs to go in the normal/ and profile/ directories, respectively.
For testing, I've cut back to just a single source file, t.c, which has a main()
in it. My SConstruct
file is in the same directory, and looks like this:
normal = DefaultEnvironment(tools=['mingw'], CCFLAGS = '-O2')
normal.VariantDir('release', '.', duplicate=0)
normal.Program('t', ['t.c'])
#profile = normal.Clone(CCFLAGS='-O2 -pg', LINKFLAGS = '-pg')
#profile.VariantDir('profile', '.', duplicate=0)
#profile.Program('t', ['t.c'])
When I run scons, I'm expecting it to put t.o and t.exe into release/, but it puts them in the current directory. And I can't run it at all with the 3 profile lines uncommented -- if I do, I get this error:
scons: *** Two environments with different actions were specified for the same target: t.o
Basically, I'm unsure why my VariantDir() calls aren't telling scons to put the output in the specified output directory, release
.
(I've read a fair bit in the docs and newsgroups, but nothing that answers this question. The closest I've come is this page, which describes a similar thing, but it involves a separate src/ directory and two separate scons files, and importing/exporting variables between them. That doesn't seem pleasant.)
回答1:
Yes, VariantDir is confusing in scons. Although not well advertised, you can put both SConstruct and SConscript in the same directory, using the current directory as the source directory
# SConstruct
SConscript('SConscript', build_dir='build', src='.')
and
# SConscript
Program('main.c')
I have never found a way to avoid using two files while keeping my sanity trying to understand variant dir :)
回答2:
I was able to separate binaries in a build directory using this call:
# SConstruct
SConscript('SConscript', variant_dir='build', src_dir='..', duplicate=0)
If you want to put binaries into a directory two levels below, do this:
# SConstruct
SConscript('SConscript', variant_dir='build/release', src_dir='../..', duplicate=0)
Basically, provide the src_dir
parameter as a path from your build directory back to your source directory.
回答3:
As http://www.scons.org/wiki/VariantDir%28%29 said,
Note that when you're not using an SConscript file in the src subdirectory, you must actually specify that the program must be built from the build/hello.c file that SCons will duplicate in the build subdirectory.
VariantDir('release','.',duplicate=0)
env=Environment()
env.Program('release/t',['release/t.c'])
when I run it with scons
on Linux.
scons -u .
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: release
gcc -o release/t.o -c t.c
gcc -o release/t release/t.o
scons: done building targets.
I guess it would also work on Win32
来源:https://stackoverflow.com/questions/1074062/why-does-scons-variantdir-not-put-output-in-the-given-directory