Using cmake to build geant4 + root project

Hi,

I’v been using the GNUmakefile to build my project which worked well, but would like to switch to cmake because I would like to use cpputest to write tests.

I attached the GNUmakefile which works and the CMakeLists.txt.

The error I get is:

$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/augre/Documents/GitHub/spinotronFinal 2
$ make -f Makefile
Scanning dependencies of target Spinotron
[  7%] Building CXX object CMakeFiles/Spinotron.dir/Spinotron.cc.o
/home/augre/Documents/GitHub/spinotronFinal 2/Spinotron.cc:38:24: fatal error: TStopwatch.h: No such file or directory
 #include <TStopwatch.h>
                        ^
compilation terminated.
make[2]: *** [CMakeFiles/Spinotron.dir/Spinotron.cc.o] Error 1
make[1]: *** [CMakeFiles/Spinotron.dir/all] Error 2
make: *** [all] Error 2

GNUmakefile.txt (1.46 KB)
CMakeLists.txt (2.65 KB)

If you use the CMake native way of doing things will be certainly easier. Have a look at root.cern.ch/how/integrate-root … ject-cmake

In summary something like this should work:

# You need to tell CMake where to find the ROOT installation. This can be done in a number of ways:
#   - ROOT built with classic configure/make use the provided $ROOTSYS/etc/cmake/FindROOT.cmake
#   - ROOT built with CMake. Add in CMAKE_PREFIX_PATH the installation prefix for ROOT
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED COMPONENTS RIO)

#---Define useful ROOT functions and macros (e.g. ROOT_GENERATE_DICTIONARY)
include(${ROOT_USE_FILE})

include_directories(${ROOT_INCLUDE_DIRS})

...

#----------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#
add_executable(Spinotron Spinotron.cc ${sources} ${headers})
target_link_libraries(Spinotron ${Geant4_LIBRARIES})
target_link_libraries(Spinotron ${ROOT_LIBRARIES})
target_link_libraries(Spinotron ${CADMESH_LIBRARIES})
#

Thank you! I did not find that page while googling.

I get this error message now:

$ cmake .
CMake Error at CMakeLists.txt:25 (find_package):
  By not providing "FindROOT.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "ROOT", but
  CMake did not find one.

  Could not find a package configuration file provided by "ROOT" with any of
  the following names:

    ROOTConfig.cmake
    root-config.cmake

  Add the installation prefix of "ROOT" to CMAKE_PREFIX_PATH or set
  "ROOT_DIR" to a directory containing one of the above files.  If "ROOT"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!

CMakeLists.txt

##----------------------------------------------------------------------------
# Setup the project
#
cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(Spinotron)

#----------------------------------------------------------------------------
# Find Geant4 package, activating all available UI and Vis drivers by default
# You can set WITH_GEANT4_UIVIS to OFF via the command line or ccmake/cmake-gui
# to build a batch mode only executable
#
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()

# You need to tell CMake where to find the ROOT installation. This can be done in a number of ways:
#   - ROOT built with classic configure/make use the provided $ROOTSYS/etc/cmake/FindROOT.cmake
#   - ROOT built with CMake. Add in CMAKE_PREFIX_PATH the installation prefix for ROOT
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED COMPONENTS RIO)

#---Define useful ROOT functions and macros (e.g. ROOT_GENERATE_DICTIONARY)
include(${ROOT_USE_FILE})

include_directories(${ROOT_INCLUDE_DIRS})

include(${Geant4_USE_FILE})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${ROOT_INCLUDE_DIR})
set(ROOT_LIBRARIES -L${ROOT_LIBRARY_DIR} -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic)
#set(CADMESH_LIBRARIES -L/usr/local/lib -lcadmesh)
# 
# SET(GCC_COVERAGE_COMPILE_FLAGS "-w -DPHASESPACE")
SET(GCC_COVERAGE_COMPILE_FLAGS "-w")
SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )
#----------------------------------------------------------------------------
# Locate sources and headers for this project
# NB: headers are included so they will show up in IDEs
#
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.hh)
file(GLOB binfiles ${PROJECT_SOURCE_DIR}/bin/*)

#file(COPY ${binfiles} DESTINATION ${PROJECT_BINARY_DIR})

#----------------------------------------------------------------------------
# Add the executable, and link it to the Geant4 libraries
#
add_executable(Spinotron Spinotron.cc ${sources} ${headers})
target_link_libraries(Spinotron ${Geant4_LIBRARIES})
target_link_libraries(Spinotron ${ROOT_LIBRARIES})
target_link_libraries(Spinotron ${CADMESH_LIBRARIES})
#----------------------------------------------------------------------------
# For internal Geant4 use - but has no effect if you build this
# example standalone
#
add_custom_target(MAIN DEPENDS Spinotron)

#----------------------------------------------------------------------------
# Install the executable to 'bin' directory under CMAKE_INSTALL_PREFIX
#
install(TARGETS Spinotron DESTINATION bin)

How did you install ROOT?
It was build with CMake?
Did you setup a running environment?
Is ROOTSYS defined?

[quote=“mato”]How did you install ROOT?
It was build with CMake?[/quote]
I can’t remember which method I used on this machine. Is there a way to determine?
It is installed under:
$ which root
/home/augre/bin/root-6.05.02/bin/root

[quote=“mato”]Did you setup a running environment?
Is ROOTSYS defined?[/quote]

I think so, the root environment variables are set and root works.

ROOTSYS is defined.
$ echo $ROOTSYS
/home/augre/bin/root-6.05.02

If the directory $ROOTSYS/cmake exists then is build by CMake. I am assuming the it is not, otherwise it would have worked out of the box. In this case you need to add

list(APPEND CMAKE_MODULE_PATH $ENV{ROOTSYS}/etc/cmake)

In any case my recommendation would be to build with CMake and you will have less problems.

Hi,

I am now using cmake to build on an other computer.

CMake Error at CMakeLists.txt:30 (include):
  include called with wrong number of arguments.  include() only takes one
  file.

That line is this

include(${ROOT_USE_FILE})

I think this means that ROOT_USE_FILE is empty. I just don’t know why, because the environment is set up and cmake finds root.

root version is 5.34-32

Probably the wrong FindROOT.cmake or ROOTConfig.cmake is called. To debug it you can perhaps issue the following command:

cmake --trace . 2>&1  | grep ROOT_LIBRARIES

Normally you should see that ROOT_LIBRARIES is set by $ROOTSYS/cmake/ROOTConfig.cmake