cmake 为 gtest 更改编译器

cmake change compiler for gtest

我正在尝试在我的 cmake 中为我的 gtest 设置编译器。
我先用FetchContent获取gtest代码。
要更改 FetchContent 中的编译器,我设置 CMAKE_CXX_COMPILER(此处回答:https://cmake.org/pipermail/cmake/2019-March/069206.html

set(CMAKE_CXX_COMPILER "/usr/bin/arm-linux-gnueabihf-g++" CACHE INTERNAL "")

########################################
# Fetch gtests
include(FetchContent)
FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/release-1.11.0.zip
)

FetchContent_MakeAvailable(googletest)
include(GoogleTest)
########################################

第一次cmake构建成功(没有CMakeCache.txt时)。但是第二个失败了:

-- 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/arm-linux-gnueabihf-g++

在那条错误消息之后,cmake 再次启动,但失败了,因为它没有获得任何 cmake 参数:

-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- 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/arm-linux-gnueabihf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at src/CMakeLists.txt:36 (message):
  Please provide spi mode.  -DSPI_MODE=[mock, asic]


-- Configuring incomplete, errors occurred!
See also "/work/build/arm/cmake/CMakeFiles/CMakeOutput.log".

设置 gtest 编译器的正确方法是什么?

我通过从 cmake 运行 命令设置编译器解决了这个问题:

cmake -DCMAKE_CXX_COMPILER=/usr/bin/arm-linux-gnueabihf-g++ ..