I am building on Centos 7, which natively has gcc 4.8.5, using CMake 3.16.6 and Ninja. My code requires gcc 7 so I specify RH devtoolset-7:
-DCMAKE_CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/g++
My code checks the compiler version as follows:
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.4.0)
message(FATAL_ERROR "g++ (GCC) version must be at least 5.4.0 (detected ${CMAKE_CXX_COMPILER_VERSION}). If on Centos 7, have you enabled Red Hat Developer ToolSet7? (-DCMAKE_CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/g++)")
endif()
endif()
I want to handle this automatically so that the user doesn’t need to worry about which Linux platform he/she is using. Do I need to check the version of gcc myself in CMakeLists.txt as shown above, and adapt the shown code to change the compiler path, or should CMake detect devtoolset-7 automatically?