15.4. The VariantDir Function

Use the VariantDir function to establish that target files should be built in a separate directory from the source files:

VariantDir('build', 'src')
env = Environment()
env.Program('build/hello.c')
      

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.

When using the VariantDir function directly, SCons still duplicates the source files in the variant directory by default:

% ls src
hello.c
% scons -Q
cc -o build/hello.o -c build/hello.c
cc -o build/hello build/hello.o
% ls build
hello  hello.c  hello.o

You can specify the same duplicate=0 argument that you can specify for an SConscript call:

VariantDir('build', 'src', duplicate=0)
env = Environment()
env.Program('build/hello.c')
      

In which case SCons will disable duplication of the source files:

% ls src
hello.c
% scons -Q
cc -o build/hello.o -c src/hello.c
cc -o build/hello build/hello.o
% ls build
hello  hello.o