Error trying to use all ROOT CERN functionalities in my Geant4 code

Hi community.

I am trying to use all CERN ROOT functionalities in my Geant4 code. To do this, I added these lines to the Geant4 CMakeLists.txt file to specify the location of my CERN ROOT installation directory, etc.:

#----------------------------------------------------------------------------------------------------------

option(WITH_ROOT "Build DRAGON with ROOT" ON)
if(WITH_ROOT)
  find_package(ROOT REQUIRED)
   if(ROOT_FOUND)
     STRING(REGEX MATCH "6.*" VERSION6MATCH ${ROOT_VERSION})
     if(NOT VERSION6MATCH)
       message(FATAL_ERROR "${name} requires ROOT 6")
     endif()
   endif()

   if(EXISTS "${ROOT_DIR}/modules/RootNewMacros.cmake")
     include("${ROOT_DIR}/modules/RootNewMacros.cmake")
   endif()
endif()
 
if(WITH_ROOT)
   include_directories(${ROOT_INCLUDE_DIRS})
   target_link_libraries(DRAGON ${ROOT_LIBRARIES} ${Geant4_LIBRARIES})
   #link_directories(${ROOT_LIBRARY_DIR})
endif()

#----------------------------------------------------------------------------------------------------------

and I get a lot of errors:

#----------------------------------------------------------------------------------------------------------
lcharon1991@lcharon1991-Lenovo-G40-70:~/Geant4/geant4work/BM3/DRAGON/DRAGON-build$ make
[ 2%] Building CXX object CMakeFiles/DRAGON.dir/src/DRAGONPrimaryGeneratorAction.cc.o
In file included from /home/lcharon1991/Documentos/ROOT/root_build/include/TString.h:28,
from /home/lcharon1991/Documentos/ROOT/root_build/include/TNamed.h:26,
from /home/lcharon1991/Documentos/ROOT/root_build/include/TAxis.h:24,
from /home/lcharon1991/Documentos/ROOT/root_build/include/TH1.h:24,
from /home/lcharon1991/Documentos/ROOT/root_build/include/TH1F.h:24,
from /home/lcharon1991/Geant4/geant4work/BM3/DRAGON/DRAGON/src/DRAGONPrimaryGeneratorAction.cc:46:
/home/lcharon1991/Documentos/ROOT/root_build/include/ROOT/RStringView.hxx:32:10: error: conflicting declaration of template ‘template<class _CharT, class _Traits> using basic_string_view = std::experimental::__ROOT::basic_string_view<_CharT, _Traits>’
32 | using basic_string_view = ::std::experimental::basic_string_view<_CharT,_Traits>;
| ^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/basic_string.h:48,
from /usr/include/c++/9/string:55,
from /usr/include/c++/9/bits/locale_classes.h:40,
from /usr/include/c++/9/bits/ios_base.h:41,
from /usr/include/c++/9/ios:42,
from /usr/include/c++/9/istream:38,
from /usr/include/c++/9/sstream:38,
from /usr/include/c++/9/complex:45,
from /home/lcharon1991/Geant4/Geant4/geant4_install/include/Geant4/G4Types.hh:74,
from /home/lcharon1991/Geant4/Geant4/geant4_install/include/Geant4/G4ios.hh:35,
from /home/lcharon1991/Geant4/Geant4/geant4_install/include/Geant4/globals.hh:35,
from /home/lcharon1991/Geant4/geant4work/BM3/DRAGON/DRAGON/include/DRAGONPrimaryGeneratorAction.hh:34,
from /home/lcharon1991/Geant4/geant4work/BM3/DRAGON/DRAGON/src/DRAGONPrimaryGeneratorAction.cc:30:
/usr/include/c++/9/string_view:90:11: note: previous declaration ‘template<class _CharT, class _Traits> class std::basic_string_view’
90 | class basic_string_view
| ^~~~~~~~~~~~~~~~~
.
.
.
#---------------------------------------------------------------------------------------------------------------------------

How can I solve this issue?

Some extra and I think useful information:

  1. geant4-config --version
    11.2.1
  2. root-config --version
    6.22/09
  3. lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 20.04.6 LTS
    Release: 20.04
    Codename: focal
  4. g++ --version
    g++ (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  5. clang++ --version
    clang version 10.0.0-4ubuntu1
    Target: x86_64-pc-linux-gnu
    Thread model: posix
    InstalledDir: /usr/bin

Thanks a lot in advance.

This looks like a consequence of C++ version mismatch to me. See also this explanation: Building ROOT from source - ROOT

You need to build your application with the same C++ standard that ROOT war compiled with. If you want to know which C++ standard that was, look at the output of root-config --cflags.

So set the C++ standard in your application accordingly, set the CMAKE_CXX_STANDARD variable in CMake correctly.

Let us know if there was a C++ standard mismatch and if resolving this helped to fix the problem. Otherwise, I have to think more about what could be wrong here.

Hi Jonas
Thanks for your response.
I checked the C++ standards for ROOT and Geant4 on my PC using the following commands:

  1. lcharon1991@lcharon1991-Lenovo-G40-70:~$ root-config --cflags
    -pthread -std=c++11 -m64 -I/home/lcharon1991/Documentos/ROOT/root_build/include
  2. geant4-config --cflags
    -I/home/lcharon1991/Documentos/ROOT/root_build/include -DG4VIS_USE_OPENGL -DG4UI_USE_TCSH -DG4UI_USE_QT -DG4VIS_USE_OPENGLQT -DG4VIS_USE_TOOLSSG_QT_GLES -I/usr/include/x86_64-linux-gnu/qt5/ -I/usr/include/x86_64-linux-gnu/qt5/QtCore -I/usr/lib/x86_64-linux-gnu/qt5//mkspecs/linux-g++ -I/usr/include/x86_64-linux-gnu/qt5/QtGui -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets -I/usr/include/x86_64-linux-gnu/qt5/QtOpenGL -DG4VIS_USE_RAYTRACERX -DG4VIS_USE_OPENGLX -DG4VIS_USE_TOOLSSG_X11_GLES -W -Wall -pedantic -Wno-non-virtual-dtor -Wno-long-long -Wwrite-strings -Wpointer-arith -Woverloaded-virtual -Wno-variadic-macros -Wshadow -pipe -pthread -ftls-model=initial-exec -std=c++17 -I/home/lcharon1991/Geant4/Geant4/geant4_install/include/Geant4 -I/home/lcharon1991/Geant4/CLHEP/clhep_install/include

I noticed the standards differ. According to the link you shared:

applications that depend on ROOT must be compiled with the same C++ standard with which ROOT was compiled

  • First option:
    Could I resolve this by adding set(CMAKE_CXX_STANDARD 11) to Geant4’s CMakeLists.txt and recompiling it?

  • Second option:
    Or by adding set(CMAKE_CXX_STANDARD 17) to the ROOT CMakeLists.txt file and recompiling it?

Based on this link, Dependencies - ROOT it seems C++17 is supported for ROOT versions after to 6.30, so I may need to update my ROOT version.

Is that correct?
Thanks.

I would recommend you to use a newer C++ standard in ROOT, because there I know better what is going on.

You can re-build also the current ROOT version that you are using with CMAKE_CXX_STANDARD 17. The C++ standards listed in the page you linked are only the minimum supported C++ standards by the different ROOT versions. For example, ROOT 6.30 can’t be built with any standard older than C++17 anymore.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.