2.2.  Building and Installing on Windows Platforms

The easiest way to build and install Geant4 from source on Windows platforms is to use the Windows command line program cmd plus CMake's command line interface to the MSBuild tool supplied with Visual Studio. Whilst the full Visual Studio GUI can be used, cmd and CMake/MSBuild provide a simpler interface and the commands can be used inside scripts. You can also use PowerShell instead of cmd if you prefer, and the instructions below should transfer barring minor syntax differences. Builds of Geant4 using Cygwin or MinGW with their own compilers or the Microsoft C++ Compiler are neither supported or tested, though the CMake system is expected to work under these toolchains. If you are using these tools via their native shells and with their own versions of CMake, then the instructions for Section 2.1 can be used.

To ensure that the appropriate Visual Studio paths and settings are set up for building, open a Visual Studio Developer cmd window from StartAll ProgramsVisual Studio 2015Visual Studio ToolsDeveloper Command Prompt for VS2015. After running this, confirm that you have the MSVC compiler available by running the cl command, and you should see (NB, in the following, the cmd prompt is shown as a > for clarity):

    > cl
    Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86
    Copyright (C) Microsoft Corporation.  All rights reserved.

    usage: cl [ option... ] filename... [ /link linkoption... ]
  

The exact version number of cl may differ slightly, but for Visual Studio 2015 the first element of the Compiler Version should be at least 19.

To begin building Geant4, unpack the source package geant4_10_03.zip to a location of your choice. For illustration only, this guide will assume it's been unpacked in a directory named C:\Users\YourUsername\Geant4, so that the Geant4 source package sits in a subdirectory

    C:\Users\YourUsername\Geant4\geant4_10_03
  

We refer to this directory as the source directory. The next step is to create a directory in which to configure and run the build and store the build products. This directory should not be the same as, or inside, the source directory. In this guide, we create this build directory alongside our source directory:

    > cd %HOMEPATH%\Geant4
    > dir /B
    geant4_10_03

    > mkdir geant4_10_03-build
    > dir /B
    geant4_10_03
    geant4_10_03-build

  

To configure the build, change into the build directory and run CMake:

    > cd %HOMEPATH%\Geant4\geant4_10_03-build
    > cmake -DCMAKE_INSTALL_PREFIX=%HOMEPATH%\Geant4\geant4_10_03-install %HOMEPATH%\Geant4\geant4_10_03
  

Here, the CMake Variable CMAKE_INSTALL_PREFIX is used to set the install directory, the directory under which the Geant4 libraries, headers and support files will be installed. It must be supplied as an absolute path. The second argument to CMake is the path to the source directory. In this example, we have used the absolute path to the source directory, but you can also use the relative path from your build directory to your source directory.

Additional arguments may be passed to CMake to activate optional components of Geant4, such as visualization drivers, or tune the build and install parameters. See Section 2.3 for details of these options. If you run CMake and decide afterwards you want to activate additional options, simply rerun CMake in the build directory, passing it the extra options plus the build directory. For example, after running CMake as above, you may wish to activate the installation of Geant4's datasets, so you would run

    > cd %HOMEPATH%\Geant4\geant4_10_03-build
    > cmake -DGEANT4_INSTALL_DATA=ON .
  

On executing the CMake command, it will run to configure the build and generate Visual Studio Project files to perform the actual build. CMake has the capability to generate buildscripts for other tools, such as NMake and Ninja, but please note that these are not supported for builds of Geant4 on Windows yet. With Visual Studio 2015, you should see output similar to

    > cmake -DCMAKE_INSTALL_PREFIX=%HOMEPATH%\Geant4\geant4_10_03-install %HOMEPATH%\Geant4\geant4_10_03
    -- Building for: Visual Studio 14 2015
    -- The C compiler identification is MSVC 19.0.23026.0
    -- The CXX compiler identification is MSVC 19.0.23026.0
    -- Check for working C compiler using: Visual Studio 14 2015
    -- Check for working C compiler using: Visual Studio 14 2015 -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working CXX compiler using: Visual Studio 14 2015
    -- Check for working CXX compiler using: Visual Studio 14 2015 -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Looking for dlfcn.h
    -- Looking for dlfcn.h - not found
    -- Looking for fcntl.h
    -- Looking for fcntl.h - found
    -- Looking for inttypes.h
    -- Looking for inttypes.h - found
    -- Looking for memory.h
    -- Looking for memory.h - found
    -- Looking for stdint.h
    -- Looking for stdint.h - found
    -- Looking for stdlib.h
    -- Looking for stdlib.h - found
    -- Looking for strings.h
    -- Looking for strings.h - not found
    -- Looking for string.h
    -- Looking for string.h - found
    -- Looking for sys/stat.h
    -- Looking for sys/stat.h - found
    -- Looking for sys/types.h
    -- Looking for sys/types.h - found
    -- Looking for unistd.h
    -- Looking for unistd.h - not found
    -- Looking for getpagesize
    -- Looking for getpagesize - not found
    -- Looking for bcopy
    -- Looking for bcopy - not found
    -- Looking for memmove
    -- Looking for memmove - found
    -- Looking for mmap
    -- Looking for mmap - not found
    -- Looking for 4 include files stdlib.h, ..., float.h
    -- Looking for 4 include files stdlib.h, ..., float.h - found
    -- Check if the system is big endian
    -- Searching 16 bit integer
    -- Looking for stddef.h
    -- Looking for stddef.h - found
    -- Check size of unsigned short
    -- Check size of unsigned short - done
    -- Using unsigned short
    -- Check if the system is big endian - little endian
    -- Looking for off_t
    -- Looking for off_t - not found
    -- Looking for size_t
    -- Looking for size_t - not found
    -- Check size of off64_t
    -- Check size of off64_t - failed
    -- Looking for fseeko
    -- Looking for fseeko - not found
    -- Looking for unistd.h
    -- Looking for unistd.h - not found
    -- Pre-configuring dataset G4NDL (4.5)
    -- Pre-configuring dataset G4EMLOW (6.50)
    -- Pre-configuring dataset PhotonEvaporation (4.2)
    -- Pre-configuring dataset RadioactiveDecay (5.1)
    -- Pre-configuring dataset G4NEUTRONXS (1.4)
    -- Pre-configuring dataset G4PII (1.3)
    -- Pre-configuring dataset RealSurface (1.0)
    -- Pre-configuring dataset G4SAIDDATA (1.1)
    -- Pre-configuring dataset G4ABLA (3.0)
    -- Pre-configuring dataset G4ENSDFSTATE (2.1)
      *WARNING*
      Geant4 has been pre-configured to look for datasets
      in the directory:

      /Users/YourUsername/Geant4/geant4_10_03-install/share/Geant4-10.3.0/data

      but the following datasets are NOT present on disk at
      that location:

      G4NDL (4.5)
      G4EMLOW (6.50)
      PhotonEvaporation (4.2)
      RadioactiveDecay (5.1)
      G4NEUTRONXS (1.4)
      G4PII (1.3)
      RealSurface (1.0)
      G4SAIDDATA (1.1)
      G4ABLA (3.0)
      G4ENSDFSTATE (2.1)

      If you want to have these datasets installed automatically
      simply re-run cmake and set the GEANT4_INSTALL_DATA
      variable to ON. This will configure the build to download
      and install these datasets for you. For example, on the
      command line, do:

      cmake -DGEANT4_INSTALL_DATA=ON <otherargs>

      The variable can also be toggled in ccmake or cmake-gui.
      If you're running on a Windows system, this is the best
      solution as CMake will unpack the datasets for you
      without any further software being required

      Alternatively, you can install these datasets manually
      now or after you have installed Geant4. To do this,
      download the following files:

      http://geant4.cern.ch/support/source/G4NDL.4.5.tar.gz
      http://geant4.cern.ch/support/source/G4EMLOW.6.50.tar.gz
      http://geant4.cern.ch/support/source/G4PhotonEvaporation.4.2.tar.gz
      http://geant4.cern.ch/support/source/G4RadioactiveDecay.5.1.tar.gz
      http://geant4.cern.ch/support/source/G4NEUTRONXS.1.4.tar.gz
      http://geant4.cern.ch/support/source/G4PII.1.3.tar.gz
      http://geant4.cern.ch/support/source/RealSurface.1.0.tar.gz
      http://geant4.cern.ch/support/source/G4SAIDDATA.1.1.tar.gz
      http://geant4.cern.ch/support/source/G4ABLA.3.0.tar.gz
      http://geant4.cern.ch/support/source/G4ENSDFSTATE.2.1.tar.gz

      and unpack them under the directory:

      /Users/YourUsername/Geant4/geant4_10_03-install/share/Geant4-10.3.0/data

      As we supply the datasets packed in gzipped tar files,
      you will need the 'tar' utility to unpack them.

      Nota bene: Missing datasets will not affect or break
      compilation and installation of the Geant4
      libraries.

    -- The following Geant4 features are enabled:
    GEANT4_BUILD_CXXSTD: Compiling against C++ Standard '11'

    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/Users/YourUsername/Geant4/geant4_10_03-build

  

The output will differ slightly due to the source/build paths being for illustration only, but the last three lines at least should be the same to within path differences. These indicate a successful configuration.

The warning message about datasets is simply an advisory. Due to the size of the datasets, Geant4 will try and reuse any datasets it can find under the data installation prefix, in our example case C:\Users\YourUsername\Geant4\geant4_10_03-install\share\Geant4-10.3.0\data. If any datasets are not found here, the message is emitted to advise you on the steps you need to take to manually install the datasets at a time of your convenience.

Datasets are not required to be present to build Geant4, but may be required to run your application, depending on the physics models you use. If you wish to download and install the datasets automatically as part of your build of Geant4, simply add the option -DGEANT4_INSTALL_DATA=ON to the arguments passed to CMake. Note that this requires a working network connection and will download around 0.5GB of data. If you already have the datasets present on your system, you can point Geant4 to their location. See the GEANT4_INSTALL_DATADIR option described Section 2.3.1 for more details.

If you see any errors at this point, carefully check the error messages output by CMake, and check your install of CMake and Visual Studio first. The default configuration of Geant4 is very simple, and provided CMake and Visual Studio are installed correctly, you should not see errors.

After the configuration has run, CMake will have generated Visual Studio Solution files for building Geant4. CMake itself can be used to run the build by executing the command

    > cmake --build . --config Release
  

Here, the --build argument takes the path to the build directory, in this case we are running from the build directory so it is just the current working directory. The --config argument takes the configuration we want to build (Visual Studio, unlike Make, can support multiple configurations in the same project) and Release is chosen to provide fully optimized libraries for best performance. If you are developing applications and require debugging information, then you should change this argument to RelWithDebInfo.

The build will now run, and will output information on the progress of the build and current operations. By default, Visual Studio Solutions do not enable parallel compilation of files for faster builds. Geant4's CMake system provides an option to enable this, so if you have a multicore system, you can add the option -DGEANT4_BUILD_MSVC_MP=ON to the arguments passed to CMake. Once the build has completed the headers, libraries and support files can be installed by running the command

    > cmake --build . --config Release --target install
  

This command may also be invoked immediately after configuration to build and install Geant4 in one step. The file and directory structure of the installation follows that of the Unix build, and is described in Section 3.1.