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 generated scripts 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.6
    $ mkdir geant4.9.6-build-release geant4.9.6-build-debug
    $ cd geant4.9.6-build-release
    $ cmake -DCMAKE_BUILD_TYPE=Release ../geant4.9.6

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

    $ make -j8
    
    ...output...
  

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

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

    ... incremental build ...

    $ cd /path/geant4.9.6-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, some IDEs (Xcode or Visual Studio for example) automatically support multiple build modes such as Release and Debug. In this case, you do not need separate build directories as the IDE handles this for you. However, you would still need two separate build directories if you, for example, wanted to develop and test against two versions of an external package such as Xerces-C.