Geant4: cmake vs. make

1. make


  • Only one step required to compile, make
  • No need to maintain a CMakeLists.txt file

To use make, you need to source the file, for example put the following in your .bash_profile:
. ~/Geant4/geant4.9.6-build/

Then, just go to the source directory and run make.

Variables can be set in you environment. For instance, if you run (or put in your bash_profile)

it should compile using Root histograms (assuming your Geant4 code supports this).

2. cmake


  • Can generate projects for IDEs, for example Xcode or Eclipse
  • Nice separation of build and source directories. So, for example, you can have a single source directory and several build directories for different versions of Geant4. You can feel free to throw away your build directory without destroying your source. Also, you can commit your source to SVN without it being polluted by compiled files
  • Seems to be the method that the Geant4 developers prefer.
  • If you do need to manually tinker, CMakeLists.txt files are far more understandable than make files

To use make, you need to source the file, for example put the following in your .bash_profile:
. ~/Geant4/geant4.9.5-install/bin/

Then, if you want to compile, first tweak your CMakeLists.txt file (see below for example). Then make a separate, blank build directory, and run a command like the following:
cmake -DGeant4_DIR=<your geant4 install directory>/lib/Geant4-9.5.1/ <your source directory>

in my case this could be for example:
cmake -DGeant4_DIR=~/Geant4/geant4.9.6-install/lib/Geant4-9.5.1/ ../code

To create an Xcode project:
cmake -G Xcode -DGeant4_DIR=~/Geant4/geant4.9.6-install/lib/Geant4-9.5.1/ ../code

It can be useful to create an alias and put it in your .bash_profile file:
cmakeGX='cmake -G Xcode -DGeant4_DIR=~/Geant4/geant4.9.5-install/lib/Geant4-9.5.1/'

then you only need to run
cmakeGX ../code

The cmake command generates makefiles, so if you are not using an IDE, then you still need to run make in your build folder to create an executable, for example:
make rdecay02

Example of CmakeLists.txt file

The following file generates the rdecay02 example, with added linking to visualisation libraries and the ROOT framework. It assumes a FindROOT.cmake file is present within a folder called cmake in the project source directory. The easiest way to get this is to copy from the Geant4 source: e.g. copy the contents geant4.9.5.p02/cmake/Modules into your cmake/ folder.

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

set(name rdecay02)

option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON)
  find_package(Geant4 REQUIRED ui_all vis_all)
  find_package(Geant4 REQUIRED)


# Find ROOT (optional package)
find_package(ROOT QUIET)
  message(STATUS "ROOT found. --> compiling with ROOT enabled.") 
  # Uncomment this line if suitable (some customization may be needed)
  message(STATUS "G4 ROOT NOT found. --> ROOT disabled.") 

# Locate sources and headers for this project
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
# the following line makes the headers visible in IDEs, for example Xcode
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)

add_executable(${name} EXCLUDE_FROM_ALL ${name}.cc ${sources} ${headers})
target_link_libraries(${name} ${Geant4_LIBRARIES} ${ROOT_LIBRARIES})