CMake cxx_std_* propagation


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.16+
Platform: M1 macOS with Docker Fedora 41 (personal); RHEL (Collaboration)
Compiler: g++ (GCC) 14.2.1 20240912 (Red Hat 14.2.1-3)


Hello ROOT experts,
I am trying to update our collaborations CMakeLists.txt.

From the example,

# CMakeLists.txt for the "event" package. It creates a library and a main program.
# If ROOT is not installed in a default system location you need to tell CMake where to find it.
# Sourcing `thisroot.sh` already sets the required environment variables.
# Otherwise, you must tell the build system where to look for ROOT,
# for example by passing `-DROOT_DIR="/path/to/root/installation` at CMake configuration time.

   cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
   project(event)

# Locate the ROOT package and define a number of useful targets and variables.
   find_package(ROOT REQUIRED COMPONENTS RIO Net)
   include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# Create a shared library.
# Passing cmake targets such as `ROOT::RIO` as dependencies (rather than plain
# library names for example via ${ROOT_LIBRARIES}) ensures that properties such as required
# include directories and C++ standard are propagated to our libraries or executables.
# Note: To ensure compatibility with Cling, targets *must* be compiled using the
# same C++ standard as ROOT was compiled with.
   add_library(Event SHARED Event.cxx)
   target_link_libraries(Event PUBLIC ROOT::RIO ROOT::Net)

# Create the main program using the library.
   add_executable(Main MainEvent.cxx)
   target_link_libraries(Main Event)

It sounds like if I link against the libraries via the ROOT::target syntax, I should not have to worry about setting the -std=cxx${ROOT_CXX_STANDARDS} flag manually.

Is this true?

You can use include(${ROOT_USE_FILE}). Can you try:

include(${ROOT_USE_FILE})
message(STATUS "CMAKE_CXX_FLAGS = ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_CXX_STANDARD = ${CMAKE_CXX_STANDARD}")

Thank you for the replies!

Let me clarify the problem a bit more.
From this book that ROOT also references it looks to me like ${ROOT_USE_FILE} is no longer recommended?

I could get ${ROOT_CXX_STANDARD} and ${ROOT_CXX_FLAGS} just fine without using ${ROOT_USE_FILE} if I just call find_package(ROOT CONFIG).

on macOS (Docker) $ROOT_CXX_STANDARD is populated.
However, on the cluster (RHEL) it is not.

On the other hand, using all of $ROOT_CXX_FLAGS causes our software to seg fault on macOS (I’ve found that $ROOT_CXX_FLAGS contains way more compiler flags on macOS than on the cluster), so I can’t use it to obtain the standard.

M1 macOS (Docker Fedora 41, ROOT 6.34) message(STATUS $ROOT_CXX_FLAGS) returns

-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -mbranch-protection=standard -fasynchronous-unwind-tables -fstack-clash-protection -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -pipe -fsigned-char -pthread

On the cluster (ROOT 6.28):

-std=c++17 -pipe -fsigned-char -pthread