15.4. The `VariantDir` Function
Prev | Chapter 15. Separating Source and Build Trees: Variant Directories | Next

## 15.4. The `VariantDir` Function

You can also use the `VariantDir` function to establish that target files should be built in a separate directory tree from the source files:

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

When using this form, you have to tell SCons that sources and targets are in the variant directory, and those references will trigger the remapping, necessary file copying, etc. for an already established variant directory. Here is the same example in a more spelled out form to show this more clearly:

    VariantDir('build', 'src')
    env = Environment()
    env.Program(target='build/hello', source=['build/hello.c'])

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=False` argument that you can specify for an `SConscript` call:

    VariantDir('build', 'src', duplicate=False)
    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

Prev | Up | Next
15.3. Telling SCons to Not Duplicate Source Files in the Variant Directory Tree | Home | 15.5. Using `VariantDir` With an `SConscript` File