使用 cmake 更改编译器会创建无限循环

问题描述 投票:0回答:5

我尝试使用 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++")

我在项目开始时调用“PROJECT”命令之前执行此操作。 但是当我调用 cmake 时出现无限循环,我有以下输出:

-- 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++

所以我在无限循环中一次又一次地收到此消息,即使我在调用 cmake 之前删除了缓存...

编辑:解决方案非常简单:您需要使用cmake版本2.8.9并且问题不会出现。

cmake g++
5个回答
3
投票

原因可能是其他cmake模块更改了您设置的变量。所以找到cmake代码并将其放在命令之前(SET(CMAKE_CXX_COMPILER "/opt/rh/devtoolset-2/root/usr/bin/g++")),应该可以解决。

我遇到 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")

add_subdirectory(pybind11)

解决方案将其更改为:

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")

2
投票

由于这是我在 Google 上的第一个结果,我想我应该提供昆汀的替代解决方案。

如果您使用 -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake 调用 cmake 命令,则可以将 CMAKE_C_COMPILER 和 CMAKE_CXX_COMPILER 覆盖放入新的 toolchain.cmake 文件中,cmake 将正确加载覆盖而不会出现无限循环。

此解决方案为那些无法轻松升级其 cmake 的人(例如,如果您是非 root 和非 sudo 用户)、那些只是想避免升级软件包或管理其他软件包所涉及的工作的人,或者那些谁不想打扰他们的系统管理员。此外,如果您希望它动态选择,toolchain.cmake 文件仍然可以执行逻辑来查找适当或最新的开发工具集。

此解决方案不适用于那些不会或无法将 -DCMAKE_TOOLCHAIN_FILE 参数添加到 cmake 调用中的人。


1
投票

在cmake3.15上,我发现一旦删除CMakeCache文件并重做cmake,一切都很好


0
投票

解决方案很简单:你需要使用cmake版本2.8.9,问题就不会出现。


0
投票

我的解决方案

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)
© www.soinside.com 2019 - 2024. All rights reserved.