Issues with CMake when adding ROOT CERN functionality

Hi everyone,

I am trying to use all the ROOT CERN functionalities in my Geant4 code. To do that I added these lines to the Geant4 CMakeList.txt file to indicate the location of my ROOT CERN installation’s directory:

#----------------------------------------------------------------------------------------------------------
option(WITH_ROOT "Build DRAGON with ROOT" ON)
if(WITH_ROOT)
  find_package(ROOT REQUIRED Hist RIO)
# ROOT version 6 required
   if(ROOT_FOUND)
     STRING(REGEX MATCH "6.*" VERSION6MATCH ${ROOT_VERSION})
     if(NOT VERSION6MATCH)
       message(FATAL_ERROR "${name} requires ROOT 6")
     endif()
   endif()
# Include ROOT's CMake functions for dictionary generation
#  since root6.20, the file is renamed and included by default, so include
#  only when we find the *old* name
   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
  5. clang++ --version
    clang version 10.0.0-4ubuntu1
    Target: x86_64-pc-linux-gnu
    Thread model: posix
    InstalledDir: /usr/bin

Attached are the files in question.
Thanks a lot in advance.

MakeOutput.txt (8.8 KB)
CMakeLists.txt (3.3 KB)


Those versions of g++ and clang++ are fairly old considering the versions of geant4 and root-config you want to use. And that error seems to point to a mismatch in some very basic c string library functions. So the first step would be to update all of those to their most recent stable releases.

This is a great use case for conda environments to check version issues in a way that isolates debugging from your root OS.

My guess is that this is the problem reported here on the ROOT forum: Enable C++14/17 support after building ROOT - #2 by linev - Newbie - ROOT Forum

You can confirm that by running root-config --cflags and seeing what C++ standard is used. Also run the build of you application as make VERBOSE=1 to see what -std=c++XY is being used there. I suspect that the ROOT config script will print -std=c++11, in which case you will may need to rebuild/install ROOT with that standard. Check the make VERBOSE=1 output first though so see what’s actually being used.

In fact, already answered in your post on the ROOT forum here: Error trying to use all ROOT CERN functionalities in my Geant4 code - #4 by jonas - ROOT - ROOT Forum