Quentin Tealrod
Quentin Tealrod

Reputation: 343

Changing compiler with cmake creates infinite loop

I try to change the compiler with cmake :

SET(CMAKE_C_COMPILER   "/opt/rh/devtoolset-2/root/usr/bin/gcc")
SET(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/g++")

I do that in the begining of my project just before calling the "PROJECT" command. But I get an infinite loop when I call cmake, I have the following output:

-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/gcc
-- Check for working C compiler: /opt/rh/devtoolset-2/root/usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/g++
-- Check for working CXX compiler: /opt/rh/devtoolset-2/root/usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Doxygen: /usr/bin/doxygen (found version "1.6.1") 
-- Looking for C++ include tut.h
-- Looking for C++ include tut.h - found
-- [STATUS] Found tut.h
-- Configuring done
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_CXX_COMPILER= /usr/bin/c++
CMAKE_CXX_COMPILER= /usr/bin/c++

So I have this message again and again in an infinite loop, even if I delete the cache before calling cmake...

EDIT : Solution is quite simple : You need to use cmake version 2.8.9 and the problem doesnt appear.

Upvotes: 12

Views: 6923

Answers (6)

LNiederha
LNiederha

Reputation: 948

Going off this answer: First off using set(CMAKE_C(XX)_COMPILER) might not be the best solution, I encourage reading this answer for some alternatives.

Second it seems if you set your compiler before setting any project of language:

This must be done before any language is set (ie: before any project() or enable_language() command). [...]

I can't explain exactly how it works but it seems to do the trick.

Upvotes: 0

Sunding Wei
Sunding Wei

Reputation: 2214

My solution

cmake_minimum_required(VERSION 3.16)

# set the same compiler paths as our sub project
set (CMAKE_C_COMPILER "/usr/bin/clang")
SET (CMAKE_CXX_COMPILER "/usr/bin/clang++")

project(ctpd)
# now add or include other project
include(./app.cmake)

Upvotes: 0

stavros
stavros

Reputation: 11

on cmake3.15 I found out that once you remove the CMakeCache file and redo cmake it is all good

Upvotes: 1

quanly_mc
quanly_mc

Reputation: 371

The reason may be that other cmake module changes the variable you set. So find the cmake code and put it before your command (SET(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/g++")), it should be solved.

I come across the problem with pybind11 submodule as following

SET(CMAKE_C_COMPILER /usr/bin/gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall   -O3 -march=native")

add_subdirectory(pybind11)

The solution is changing it as:

add_subdirectory(pybind11)

SET(CMAKE_C_COMPILER /usr/bin/gcc)
SET(CMAKE_CXX_COMPILER /usr/bin/g++)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}  -Wall  -O3 -march=native ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall   -O3 -march=native")

Upvotes: 5

Xeon
Xeon

Reputation: 31

Since this was the first result on Google for me, I figured I'd offer an alternate solution to Quentin's.

If you call your cmake command with -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake, you can then put your CMAKE_C_COMPILER and CMAKE_CXX_COMPILER overrides in a new toolchain.cmake file and cmake will properly load the overrides without an infinite loop.

This solutions offers a fix for those who cannot easily upgrade their cmake (such as if you are a non-root and non-sudo user), those who simply want to avoid the effort involved in upgrading packages or managing additional packages, or those who do not want to bother their sysadmin. Additionally, the toolchain.cmake file can still execute logic to find the appropriate or latest devtoolset should you want it to dynamically choose it.

This solution will not work for those who will not or cannot add the -DCMAKE_TOOLCHAIN_FILE argument to the cmake call.

Upvotes: 3

Quentin Tealrod
Quentin Tealrod

Reputation: 343

Solution is quite simple : You need to use cmake version 2.8.9 and the problem doesnt appear.

Upvotes: -3

Related Questions