Integrating ROOT Using CMake

I intend to use the curve fitting algorithms of the ROOT library, as they proved useful and reliable before. However, so far I have only used the ROOT macro interpreter and never compiled a full program myself.

I have a standing C++ program into which I would like to import the ROOT library. I found this article as a starting point. I downloaded the tar-ball, untared it, set up the environment variables using /path/to/root/bin/thisroot.sh (using bash) and ran cmake in the provided folder.

The contents of the CMakeLists.txt file is:

# CMakeLists.txt for event package. It creates a library with dictionary and a main program
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(event)

# 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 MathCore RIO Hist Tree Net)

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

include_directories(${CMAKE_SOURCE_DIR} ${ROOT_INCLUDE_DIRS})
add_definitions(${ROOT_CXX_FLAGS})

ROOT_GENERATE_DICTIONARY(G__Event Event.h LINKDEF EventLinkDef.h)

#---Create a shared library with geneated dictionary
add_library(Event SHARED Event.cxx G__Event.cxx)
target_link_libraries(Event ${ROOT_LIBRARIES})

#---Create  a main program using the library
add_executable(Main MainEvent.cxx)
target_link_libraries(Main Event)

I get the following error when executing cmake:

[code]-- The C compiler identification is GNU 4.9.2
– The CXX compiler identification is GNU 4.9.2
– Check for working C compiler: /usr/bin/cc
– Check for working C compiler: /usr/bin/cc – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– 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
CMake Error at CMakeLists.txt:11 (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.
[/code]

I also tried to add the file

/path/to/root/etc/cmake/FindROOT.cmake

to the working directory and add

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})

to the CMakeLists.txt file. This results into this error:

CMake Error at FindROOT.cmake:52 (list):
  list sub-command REMOVE_DUPLICATES requires list to be present.
Call Stack (most recent call first):
  CMakeLists.txt:13 (find_package)


CMake Error at /usr/share/cmake-3.0/Modules/FindPackageHandleStandardArgs.cmake:136 (message):
  Could NOT find ROOT (missing: ROOT_CONFIG_EXECUTABLE ROOTSYS ROOT_VERSION
  ROOT_INCLUDE_DIR ROOT_LIBRARIES ROOT_LIBRARY_DIR)
Call Stack (most recent call first):
  /usr/share/cmake-3.0/Modules/FindPackageHandleStandardArgs.cmake:343 (_FPHSA_FAILURE_MESSAGE)
  FindROOT.cmake:74 (find_package_handle_standard_args)
  CMakeLists.txt:13 (find_package)

I’d be really helpful if someone could help me getting at least the example code working. I think, I will be able to work it into the cmake setup of my own program afterwards.

Best,
Philipp Verpoort

Hi Philipp,

In your last attempt did you have ROOTSYS still set? If yes, could you try setting CMAKE_PREFIX_PATH to the location of ROOT?

The build of ROOT you use - was it done with configure/make or cmake? Does it contain ${ROOTSYS}/cmake/ROOTConfig.cmake? If yes, please set CMAKE_PREFIX_PATH to point to this directory. Then you don’t need FindROOT.cmake

Benedikt

Dear Benedikt,

thank you so much for your help.

Yes, I had ROOTSYS set at all times.

I think, in the case of ‘yes’, the following command, which is already included, should do the job, or am I wrong?

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})

Er… good point. I was using configure/make.

Now, I’m using cmake and I produced a builddir and included this one into CMAKE_PREFIX_PATH:

list(APPEND CMAKE_PREFIX_PATH /path/to/root/builddir/)

The output looks better but there is still something missing. Do you know what is wrong this time?

philipp@pc:/path/to/event/build$ cmake ..
CMake Error at /path/to/root/builddir/ROOTConfig.cmake:141 (message):
  ROOT component MathCore not found
Call Stack (most recent call first):
  CMakeLists.txt:13 (find_package)


-- Configuring incomplete, errors occurred!

Thanks again,
Best,
Philipp

I fixed it. It seems the cmake procedure for ROOT didn’t run through completely the first time. Thanks for the help!

Just for completeness this the HowTo for Integrating ROOT with CMake
https://root.cern.ch/how/integrate-root-my-project-cmake

Here is the event.tar.gz file. Download it.
Go to build folder.
event$cd build
/event/build/$cmake …/ If you get error add this line to the CMakeList.txt file at Ln-8. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} $ENV{ROOTSYS}/etc/cmake)

then make

$ cmake …/
– Found ROOT: /home/rama/root-6.12.06/bin/root-config
– Configuring done
– Generating done
– Build files have been written to: /home/rama/Documents/cpp/ROOT_MACRO/CMake/event/build

$make
[ 16%] Generating G__Event.cxx
Scanning dependencies of target Event
[ 33%] Building CXX object CMakeFiles/Event.dir/Event.cxx.o
[ 50%] Building CXX object CMakeFiles/Event.dir/G__Event.cxx.o
[ 66%] Linking CXX shared library libEvent.so
[ 66%] Built target Event
Scanning dependencies of target Main
[ 83%] Building CXX object CMakeFiles/Main.dir/MainEvent.cxx.o
[100%] Linking CXX executable Main
[100%] Built target Main

Run the executable ./Main