4.3.  Building Quickly and Efficiently with Multiple Build Directories

The many ways in which Geant4 can be configured with optional components can make compilation and testing under different configurations time consuming.

As the CMake configured buildscripts live in a build directory isolated from the source directory, one can create several build directories configured against the same source directory. Each directory can have a different configuration, for example

    $ cd /path/to 
    $ ls
    geant4.9.5
    $ mkdir geant4.9.5-build-release geant4.9.5-build-debug
    $ cd geant4.9.5-build-release
    $ cmake -DCMAKE_BUILD_TYPE=Release ../geant4.9.5

    ...output...
 
    $ make -j8
    
    ...output...
    
    $ cd ../geant4-build-debug
    $ cmake -DCMAKE_BUILD_TYPE=Debug ../geant4.9.5
    
    ...output...

    $ make -j8
    
    ...output...
  

The above example uses Unix, but the same technique works on all platforms. It may not seem to have gained you much, but when you now edit and develop code that is living under /path/to/geant4.9.5, you only need to rebuild in each directory:

    ... work on code ...
    $ cd /path/to/geant4.9.5-build-release
    $ make -j8

    ... incremental build ...

    $ cd /path/geant4.9.5-build-debug
    $ make -j8

    ... incremental build ...
  

The builds pick up the changes you make to the source and build separately without needing reconfiguration. This is particularly powerful if the different configurations you need to test (for example, different versions of an external package) would require significant recompilation if the configuration were changed. Naturally, this power comes at the cost of some disk space, so may not be ideal in all cases.

Note that whilst this technique works on all platforms and buildtools, if you are using an IDE like Xcode or Visual Studio and simply want to build in the Release, Debug and other build modes, the IDE will handle this for you. These tools essentially use the above pattern behind the scenes.