Enable C++14/17 support after building ROOT

Hello,
I am trying to compile macros with c++14/17 features, either with g+±9

g++-9 -std=c++17 -o simulation.exe particle.cpp main-particle.C `root-config --cflags --libs`

or with ACLiC

.L particle.cpp+
.L main-particle.C+

Building ROOT was my first experience with CMake and I did not set the flag -Dcxx17=ON. Having used inline variables, fold expressions and deduced return types, I get error messages and warnings. I notice the flag -std=c++17 is ignored, possibly because of root-config which comes after and sets c++11 support. Therefore I tried putting it at the end of the command, which results in compile errors in several ROOT headers, starting from:

In file included from /home/kimdamiani/Products/root/include/TString.h:28,
                 from /home/kimdamiani/Products/root/include/TNamed.h:26,
                 from /home/kimdamiani/Products/root/include/TRandom.h:25,
                 from main-particle.C:2:
/home/kimdamiani/Products/root/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/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from main-particle.C:1:
/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
      |           ^~~~~~~~~~~~~~~~~

-the list goes on.

Is there any other flag I can add when compiling my macros or is deleting ROOT and building it again with -Dcxx17=ON the only option to support c++14/17 features?

Thank you in advance,
Kim

_ROOT Version: 6.18/04
_Platform: Ubuntu 19.04 x86_64

Hi,

With latest ROOT version you should use following command for ROOT compilation:

cmake -DCMAKE_CXX_STANDARD=17 ...

Please check that is output of the command:

root-config --cflags

If ROOT compiled with default settings, c++11 will overwrite your c++17 flags.

You also can move -std=c++17 after root-config --cflags.

Regards,
Sergey

1 Like

Thank you for the explanation.

I did use the default settings to compile ROOT (at the time I didn’t know I had to add the option for c++17) , so I get:

$ root-config --cflags
-pthread -std=c++11 -m64 -I/home/kimdamiani/Products/root/include

The problem is that if I move -std=c++17 at the end of the command, writing:

g++-9 -o simulation.exe particle.cpp main-particle.C `root-config --cflags --libs` -std=c++17

I get the list of errors in ROOT headers I mentioned before, for example non-matching declarations, use of the incomplete type TString, use of non-template classes as templates and entities defined in namespace std which are not qualified and therefore are undeclared.

errors-root-headers.txt (152.7 KB)

The same happens if I modify directly the --cflags variable in the root-config file, changing -std=c++11 to -std=c++17, though I understand that it may not be a good idea because there are other parts of ROOT which still have the default settings in terms of support for c++11.

EDIT: I have the same problem even if I try to compile a simple test program:

//test.C
#include "TRandom.h"
#include "TMath.h"
void generate(){}

int main(){
	generate();
	return 0;
}

with the command

g++ test.C `root-config --cflags --libs` -std=c++17

I’m afraid, you will need to build your ROOT from scratch, adding “-Dcxx17=ON” (note that, with this new version, you will not be able to “downgrade” the standard using “-std=c++14” nor “-std=c++11” command line options).

With your current ROOT version, which uses the default “-Dcxx11=ON”, you may be able to “upgrade” the standard using the “-std=c++14” command line option:

`root-config --cxx --cflags` -std=c++14 test.C `root-config --libs`

Thank you very much!