Chapter 2.  Building and Installing Geant4

2.1.  Building and Installing on Unix Platforms

Unpack the Geant4 source package geant4.9.5.tar.gz to a location of your choice. For illustration only, this guide will assume it's been unpacked in a directory /path/to, so that the Geant4 source package sits in a subdirectory

    /path/to/geant4.9.5
  

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 /path/to
    $ mkdir geant4.9.5-build
    $ ls
    geant4.9.5  geant4.9.5-build
  

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

    $ cd /path/to/geant4.9.5-build
    $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.9.5-install /path/to/geant4.9.5
  

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 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 passing it those options.

On executing this CMake command, it will run, configuring the build and generating Unix Makefiles to perform the actual build. CMake has the capability to generate buildscripts for other tools, such as Eclipse and Xcode, but please note that we do not support user installs of Geant4 with these tools. On Linux, you will see output similar to:

    $ cmake -DCMAKE_INSTALL_PREFIX=/path/to/geant4.9.5-install /path/to/geant4.9.5
    -- The C compiler identification is GNU
    -- The CXX compiler identification is GNU
    -- Check for working C compiler: /usr/bin/gcc
    -- Check for working C compiler: /usr/bin/gcc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- setting default compiler flags for CXX
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Found EXPAT: /usr/lib64/libexpat.so
    -- The following Geant4 features are enabled:
    GEANT4_USE_SYSTEM_EXPAT: Using system install of EXPAT

    -- Configuring done
    -- Generating done
    -- Build files have been written to: /path/to/geant4.9.5-build
  

On Mac OS X, the output will have slight differences, but the last three lines at least should be the same. These indicate a successful configuration.

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

After the configuration has run, CMake will have generated Unix Makefiles for building Geant4. To run the build, simply execute make in the build directory:

    $ make -jN
  

where N is the number of parallel jobs you require (e.g. if your machine has a dual core processor, you could set N to 2).

The build will now run, and will output information on the progress of the build and current operations. If you need more output to help resolve issues or simply for information, run make as

    $ make -jN VERBOSE=1
  

Once the build has completed, you can install Geant4 to the directory you specified earlier in CMAKE_INSTALL_PREFIX by running

    $ make install
  

in the build directory. The libraries, headers and resource files are installed under your chosen install prefix in a standard Unix-style hierarchy of directories, described below in Section 3.1.