Search code examples
c++c++17root-framework

how can I use CERN Root with C++17?


Apparently, CERN's "Root" software is compatible (as of version 6.12) with C++17. However, I have been completely unable to get this to work with the newest version (6.20) and all previous questions I found on this topic are from a few years ago.

Does anyone with Root experience know if there is a particular compiler option to get it to work with C++17?


Examples of errors I get when trying to compile a trivial "Hello world" program:

  • The following is (I think) due to the "TFile.h" header implicitly loading "TString.h":
/usr/local/bin/root_v6.20.02/include/ROOT/RStringView.hxx:32:84: error: conflicting declaration of template ‘template<class _CharT, class _Traits> using basic_string_view = std::experimental::__ROOT::basic_string_view<_CharT, _Traits>
  • An example of another (more mysterious, to me at least) error is:
/usr/include/c++/7/ext/concurrence.h:53:16: error: ‘_Lock_policy’ does not name a type                            
   static const _Lock_policy __default_lock_policy =                                                              
                ^~~~~~~~~~~~                                                                                      
In file included from /usr/include/c++/7/iostream:38:0,                                                           
                 from test.cpp:1:                                                                                 
/usr/include/c++/7/ext/concurrence.h: In function ‘void std::__throw_concurrence_lock_error()’:                   
/usr/include/c++/7/ext/concurrence.h:102:5: error: ‘__concurrence_lock_error’ was not declared in this scope      
   { _GLIBCXX_THROW_OR_ABORT(__concurrence_lock_error()); }                                                       
     ^                                                                                                            
/usr/include/c++/7/ext/concurrence.h:102:5: note: suggested alternative:                                          
In file included from /usr/include/c++/7/memory:74:0,                                                             
                 from /usr/local/bin/root_v6.20.02/include/ROOT/TypeTraits.hxx:15,                                
                 from /usr/local/bin/root_v6.20.02/include/TString.h:29,                                          
                 from /usr/local/bin/root_v6.20.02/include/TNamed.h:26,                                           
                 from /usr/local/bin/root_v6.20.02/include/TKey.h:15,                                             
                 from /usr/local/bin/root_v6.20.02/include/TBasket.h:28,                                          
                 from /usr/local/bin/root_v6.20.02/include/ROOT/TIOFeatures.hxx:14,                               
                 from /usr/local/bin/root_v6.20.02/include/TTree.h:30,                                            
                 from /usr/local/bin/root_v6.20.02/include/TNtuple.h:24,                                          
                 from test.cpp:2:                                                                                 
/usr/include/c++/7/ext/concurrence.h:67:9: note:   ‘__gnu_cxx::__concurrence_lock_error’                          
   class __concurrence_lock_error : public std::exception 

I am using the latest version of g++ and Root, on Linux, and have the following options in my Makefile:

CXX = g++
CXXFLAGS = -march=native `root-config --cflags --libs` -std=c++17
LDFLAGS = `root-config --cflags --libs`

Any comments about a possible workaround would be appreciated, or perhaps there is some additional option I need to add in the Makefile? It would also be helpful to know if it is advisable to stick to C++11/14 when using Root libraries.


Solution

  • You can use root only with the c++ standard with which your root version has been compiled. This is a known inconvenience and I believe the developers would be happy if/when they get this sorted out. The build configuration root-config --cflags should set the appropriate c++ standard. This might very well be c++17 (it is for me)

    pseyfert@robusta:~ > root-config --cflags
    -pthread -std=c++17 -m64 -fdiagnostics-color -march=native -Wextra -Wall -Wshadow -I/home/pseyfert/coding/root-install/include
    

    If you compile root from source, then CMAKE_CXX_STANDARD=17 or cxx17=ON (doc) are the build options you want to set to compile root with c++17 and use c++17 in all downstream projects.

    If you use a binary installation of root that switch needs to be done by whoever provides the build.