The BuildDir Function

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

      BuildDir('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 BuildDir function directly, SCons still duplicates the source files in the build directory by default:

      % ls src
      hello.c
      % scons -Q
      cc -c -o build/hello.o 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:

      BuildDir('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 -c -o build/hello.o src/hello.c
      cc -o build/hello build/hello.o
      % ls build
      hello  hello.o