我正在尝试根据 env 变量的值(如果预设)更新 cmake 变量。我也在缓存这个变量。但是,它在 cmake 中没有正确更新,但在缓存中我可以看到更新的值。
这里是我的测试 CMakeLists.txt 文件
我已经设置了环境变量
compiler_verion=clang15
cmake_minimum_required(VERSION 3.26)
project(test)
if(NOT compiler_version)
set(compiler_version "gcc")
message("++++++++++++++++++++++++++++++ default ${compiler_version}")
endif()
if(NOT "$ENV{compiler_version}" STREQUAL "")
set(compiler_version $ENV{compiler_version} CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++ env compiler_version : $ENV{compiler_version} cmake_compiler_version ${compiler_version} ++++++++++++++++++++++++++++")
elseif(NOT compiler_version)
set(compiler_version "gcc" CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++++++ else if ${compiler_version}")
else()
set(compiler_version ${compiler_version} CACHE STRING "which compiler to use..." FORCE)
message("++++++++++++++++++++++++++++++ else ${compiler_version}")
endif()
我正在运行命令
cmake ..
来自构建文件夹。
这是实际输出。
-- The C compiler identification is GNU 8.5.0
-- The CXX compiler identification is GNU 8.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
++++++++++++++++++++++++++++++ default gcc
++++++++++++++++++++++++++ env compiler_verion : clang15 cmake_compiler_version gcc ++++++++++++++++++++++++++++
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /test/buil
我的问题与此有关。
++++++++++++++++++++++++++ env compiler_version : clang15 cmake_compiler_version gcc
为什么不将
compiler_version
设置为 clang15
?
如果我在缓存文件已经可用时重新运行它,它会按预期工作。
++++++++++++++++++++++++++ env compiler_verion : clang15 cmake_compiler_version clang15 ++++++++++++++++++++++++++++
-- Configuring done (0.0s)
-- Generating done (0.0s)
我错过了什么吗? 我在linux平台上使用cmake版本3.26。
也许你应该导出环境
export compiler_version=clang15
或使用compiler_version=clang15 cmake ...
运行cmake