I'd like to set the compiler version in CMake so I can switch between GCC and Clang just by commenting out a few lines.
Below is the top of my CMake script, with the two additional lines setting GCC as the compiler:
cmake_minimum_required(VERSION 3.4.1...3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(test_project C CXX)
set (CMAKE_C_COMPILER "/usr/bin/gcc") # Adding these causes infinite loop
set (CMAKE_CXX_COMPILER "/usr/bin/g ")
message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler Vers: ${CMAKE_CXX_COMPILER_VERSION}")
However, when I add the above two lines and run cmake ..
the script enters an infinite loop, outputting:
You have changed variables that require your cache to be deleted
Configure will be re-run and you may have to reset some variables
The following variables have changed:
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_CXX_COMPILER= /usr/bin/c
CMAKE_CXX_COMPILER= /usr/bin/c
CMAKE_C_COMPILER= /usr/bin/cc
CMAKE_CXX_COMPILER= /usr/bin/c
CMAKE_C_COMPILER= /usr/bin/cc
I'd eventually like to have something like this:
cmake_minimum_required(VERSION 3.4.1...3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
project(test_project C CXX)
set (CMAKE_C_COMPILER "/usr/bin/gcc")
set (CMAKE_CXX_COMPILER "/usr/bin/g ")
#set (CMAKE_C_COMPILER "/usr/bin/clang")
#set (CMAKE_CXX_COMPILER "/usr/bin/clang ")
message(STATUS "Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "Compiler Vers: ${CMAKE_CXX_COMPILER_VERSION}")
Anyone know what's wrong?
CodePudding user response:
It seems to be an ancient CMake bug, where the workaround proposed is just putting the variables before the project
command:
cmake_minimum_required(VERSION 3.17.2)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/g ")
project(test_project C CXX)
...