CmakeVsMake
Geant4: cmake vs. make
1. make
Advantages
- Only one step required to compile, make
- No need to maintain a CMakeLists.txt file
To use make, you need to source the geant4make.sh file, for example put the following in your .bash_profile:. ~/Geant4/geant4.9.6-build/geant4make.sh
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)export G4ANALYSIS_USE_ROOT=1
it should compile using Root histograms (assuming your Geant4 code supports this).
2. cmake
Advantages
- 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 geant4.sh file, for example put the following in your .bash_profile:. ~/Geant4/geant4.9.5-install/bin/geant4.sh
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 runcmakeGX ../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) project(${name}) option(WITH_GEANT4_UIVIS "Build example with Geant4 UI and Vis drivers" ON) if(WITH_GEANT4_UIVIS) find_package(Geant4 REQUIRED ui_all vis_all) else() find_package(Geant4 REQUIRED) endif() include(${Geant4_USE_FILE}) #---------------------------------------------------------------------------- # Find ROOT (optional package) # set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) find_package(ROOT QUIET) if(ROOT_FOUND) message(STATUS "ROOT found. --> compiling with ROOT enabled.") # Uncomment this line if suitable (some customization may be needed) add_definitions(-DG4ANALYSIS_USE_ROOT) else() message(STATUS "G4 ROOT NOT found. --> ROOT disabled.") endif() #---------------------------------------------------------------------------- # Locate sources and headers for this project # include_directories(${PROJECT_SOURCE_DIR}/include ${Geant4_INCLUDE_DIR} ${ROOT_INCLUDE_DIR}) 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})