Problem linking ROOT with cmake

Hello,

I compile an application using root (and geant4) with the CMakeList in attachement.
I have the following message when I run cmake
WARNING: Target “incl” requests linking to directory “/opt/root_v5.34.02/lib”. Targets may link only to libraries. CMake is dropping the item.
And the following error message when I run make
CMakeFiles/incl.dir/incl.cc.o: In function __static_initialization_and_destruction_0(int, int)': incl.cc:(.text+0xaab): undefined reference toTVersionCheck::TVersionCheck(int)’
CMakeFiles/incl.dir/src/ExN03ROOTuple.cc.o: In function ExN03ROOTuple::ExN03ROOTuple(ExN03DetectorConstruction*)': ExN03ROOTuple.cc:(.text+0x47): undefined reference toExN03BeamIn::ExN03BeamIn()’
ExN03ROOTuple.cc:(.text+0x64): undefined reference to ExN03Datai_eventbyevent_new::ExN03Datai_eventbyevent_new()' ExN03ROOTuple.cc:(.text+0xb2): undefined reference toTROOT::TROOT(char const*, char const*, void (**)())’
ExN03ROOTuple.cc:(.text+0xc1): undefined reference to TROOT::~TROOT()' ExN03ROOTuple.cc:(.text+0xe0): undefined reference toTObject::operator delete(void*)’
ExN03ROOTuple.cc:(.text+0xf0): undefined reference to `TObject::operator delete(void*)’


Could you suggest me how to fix the problem?
Thanks in advance,
Anna
CMakeLists.txt (1.19 KB)

I have partially resolved the problem using another CMakeLists.txt (in attachement).
Now I have the following error in compilation. It seems to be related to the root header that are not found. Please note that ExN03BeamIn is my library, that I have previously compiled with a standard GNUMakefile.

acorsi@irfulx168:~/INCL$ make -j4
Scanning dependencies of target incl
[ 16%] [ 16%] [ 33%] [ 33%] Building CXX object CMakeFiles/incl.dir/incl.cc.o
Building CXX object CMakeFiles/incl.dir/src/ExN03EventActionMessenger.cc.o
Building CXX object CMakeFiles/incl.dir/src/ExN03ROOTuple.cc.o
Building CXX object CMakeFiles/incl.dir/src/ExN03PrimaryGeneratorAction.cc.o
In file included from /home/irfulx168/mnt/acorsi/INCL/include/ExN03PrimaryGeneratorAction.hh:6:0,
from /home/irfulx168/mnt/acorsi/INCL/src/ExN03PrimaryGeneratorAction.cc:1:
/home/irfulx168/mnt/acorsi/INCL/include/…/lib/ExN03BeamIn.hh:7:21: fatal error: TObject.h: No such file or directory
compilation terminated.
In file included from /home/irfulx168/mnt/acorsi/INCL/src/ExN03ROOTuple.cc:2:0:
/home/irfulx168/mnt/acorsi/INCL/include/ExN03ROOTuple.hh:5:19: fatal error: TROOT.h: No such file or directory
compilation terminated.
In file included from /home/irfulx168/mnt/acorsi/INCL/include/ExN03PrimaryGeneratorAction.hh:6:0,
from /home/irfulx168/mnt/acorsi/INCL/incl.cc:10:
/home/irfulx168/mnt/acorsi/INCL/include/…/lib/ExN03BeamIn.hh:7:21: fatal error: TObject.h: No such file or directory
compilation terminated.
make[2]: *** [CMakeFiles/incl.dir/src/ExN03PrimaryGeneratorAction.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs…
make[2]: *** [CMakeFiles/incl.dir/src/ExN03ROOTuple.cc.o] Error 1
make[2]: *** [CMakeFiles/incl.dir/incl.cc.o] Error 1
make[1]: *** [CMakeFiles/incl.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt (1.18 KB)

Hi, there are some weird things in your CMakeLists. You should not add header files to the list of source files of your incl executable, so your point 5 should look like this:

add_executable(incl incl.cc ${sources})

Next, the fact that the Root headers are not found might be related to the find_package(ROOT) instruction. To see if the ${ROOT_INCLUDE_DIRS} variable is correctly set, print it during the cmake execution:

message(${ROOT_INCLUDE_DIRS})
include_directories(${ROOT_INCLUDE_DIRS})

Finally, to debug these kind of errors it is always useful to compile with verbose output:

$ make all VERBOSE=1

This will print out the invocation of gcc with the full set of command line parameters, so that you can identify missing/wrong paths.

Hi,

Thanks for the reply. I followed the suggestions.

This is the output of cmake:
acorsi@irfulx168:~/INCL$ cmake -DGeant4_DIR=/home/irfulx168/mnt/acorsi/geant4_incl/geant4.10.00-install/lib/Geant4-10.0.0/Geant4 ~/INCL/
– Found ROOT 5.34/02 in /opt/root_v5.34.02
– Configuring done
– Generating done
– Build files have been written to: /home/irfulx168/mnt/acorsi/INCL
acorsi@irfulx168:~/INCL$ rm CMakeCache.txt
acorsi@irfulx168:~/INCL$ cmake -DGeant4_DIR=/home/irfulx168/mnt/acorsi/geant4_incl/geant4.10.00-install/lib/Geant4-10.0.0/Geant4 ~/INCL/
– 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
– 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 ROOT 5.34/02 in /opt/root_v5.34.02
– Configuring done
– Generating done
– Build files have been written to: /home/irfulx168/mnt/acorsi/INCL

and this is the output of make:
acorsi@irfulx168:~/INCL$ make all VERBOSE=1
/usr/bin/cmake -H/home/irfulx168/mnt/acorsi/INCL -B/home/irfulx168/mnt/acorsi/INCL --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/irfulx168/mnt/acorsi/INCL/CMakeFiles /home/irfulx168/mnt/acorsi/INCL/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory /home/irfulx168/mnt/acorsi/INCL' make -f CMakeFiles/incl.dir/build.make CMakeFiles/incl.dir/depend make[2]: Entering directory/home/irfulx168/mnt/acorsi/INCL’
cd /home/irfulx168/mnt/acorsi/INCL && /usr/bin/cmake -E cmake_depends “Unix Makefiles” /home/irfulx168/mnt/acorsi/INCL /home/irfulx168/mnt/acorsi/INCL /home/irfulx168/mnt/acorsi/INCL /home/irfulx168/mnt/acorsi/INCL /home/irfulx168/mnt/acorsi/INCL/CMakeFiles/incl.dir/DependInfo.cmake --color=
make[2]: Leaving directory /home/irfulx168/mnt/acorsi/INCL' make -f CMakeFiles/incl.dir/build.make CMakeFiles/incl.dir/build make[2]: Entering directory/home/irfulx168/mnt/acorsi/INCL’
/usr/bin/cmake -E cmake_progress_report /home/irfulx168/mnt/acorsi/INCL/CMakeFiles 1
[ 8%] Building CXX object CMakeFiles/incl.dir/incl.cc.o
/usr/bin/c++ -DG4_STORE_TRAJECTORY -DG4VERBOSE -DG4UI_USE -DG4VIS_USE -DG4UI_USE_TCSH -W -Wall -pedantic -Wno-non-virtual-dtor -Wno-long-long -Wwrite-strings -Wpointer-arith -Woverloaded-virtual -Wno-variadic-macros -Wshadow -pipe -std=c++98 -I/home/irfulx168/mnt/acorsi/geant4_incl/geant4.10.00-install/include/Geant4 -I/home/irfulx168/mnt/acorsi/INCL/include -o CMakeFiles/incl.dir/incl.cc.o -c /home/irfulx168/mnt/acorsi/INCL/incl.cc
In file included from /home/irfulx168/mnt/acorsi/INCL/include/ExN03PrimaryGeneratorAction.hh:6:0,
from /home/irfulx168/mnt/acorsi/INCL/incl.cc:10:
/home/irfulx168/mnt/acorsi/INCL/include/…/lib/ExN03BeamIn.hh:7:21: fatal error: TObject.h: No such file or directory
compilation terminated.
make[2]: *** [CMakeFiles/incl.dir/incl.cc.o] Error 1
make[2]: Leaving directory /home/irfulx168/mnt/acorsi/INCL' make[1]: *** [CMakeFiles/incl.dir/all] Error 2 make[1]: Leaving directory/home/irfulx168/mnt/acorsi/INCL’
make: *** [all] Error 2

Did you include the message(${ROOT_INCLUDE_DIRS}) command to your CMakeLists? I don’t see its output in cmake invocation. Also, from the verbose log of make it seems that no include directory is set for Root, which explains why you get the error. Which is the folder where you have the Root headers?
Could you also please post the cmake config file for Root? I suspect that you are possibly using a wrong variable to set the include directory, e.g. it may be called ROOT_INCLUDE_DIR without the trailing S…

Hi, thank you.

I suspect I don’t include correctly the FindROOT.cmake.
I use the command include(“FindROOT.cmake”), FindROOT.cmake is in the same directory and enclosed here.

When I run cmake I have the following output:
CMake Warning at CMakeLists.txt:12 (find_package):
Could not find module FindROOT.cmake or a configuration file for package
ROOT.

Adjust CMAKE_MODULE_PATH to find FindROOT.cmake or set ROOT_DIR to the
directory containing a CMake configuration file for ROOT. The file will
have one of the following names:

ROOTConfig.cmake
root-config.cmake

And no output for “message”.

I don’t see the FindROOT.cmake file in your message… anyway, add this command to your CmakeLists:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} <folder containing FindROOT.cmake>)

before the call to find_package(ROOT).

Hello,

Sorry about the attachment. Please find it enclosed here, together with the cmakelists
I did as you suggested and I have no more errors when I run cmake. I have the message
– Found ROOT 5.34/02 in /opt/root_v5.34.02
but no output from message.
When I run make, I have the following error:

/home/irfulx168/mnt/acorsi/INCL/include/…/lib/ExN03BeamIn.hh:7:21: fatal error: TObject.h: No such file or directory
compilation terminated.
CMakeLists.txt (1.32 KB)

I’d need the FindROOT.cmake, not the CMakeLists. It’s inside FindROOT.cmake that possibly the variable containing the Root include directory is defined, and my hypothesis is that its name is not ROOT_INCLUDE_DIRS. That would explain also why you see no output from message: because the ROOT_INCLUDE_DIRS variable does not exist. But I need FindROOT.cmake to confirm or reject this hypothesis…

Hello,

Sorry again. I think I the cmake extension is not accepted as attachement, but I did’t realize that.
Please find the FindROOT.cmake attached.

Anna
FindROOT .cmake.txt (5.83 KB)

As I guessed, the problem is that you are using the wrong variable for the Root include directory. As you can see from the top of FindROOT.cmake, it is defined as:

# ROOT_INCLUDE_DIR    PATH to the include directory

i.e., without the trailing S. So you should replace ROOT_INCLUDE_DIRS in your CMakeLists with ROOT_INCLUDE_DIR.