gpt4 book ai didi

c++ - CMake 命令行定义不会延续到工具链文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:16:00 24 4
gpt4 key购买 nike

我有一个cmake交叉编译工具链文件,简写为:

set(CMAKE_SYSTEM_NAME Linux)
if( DEFINED TC_PATH )
message( STATUS " TC_PATH IS defined. ${TC_PATH}" )
else()
message( FATAL_ERROR " TC_PATH not defined." )
endif()

set(CMAKE_C_COMPILER ${TC_PATH}/usr/bin/i586-linux/i586-linux-gcc )
set(CMAKE_CXX_COMPILER ${TC_PATH}/usr/bin/i586-linux/i586-linux-g++ )
set(CMAKE_LINKER ${TC_PATH}/usr/bin/i586-linux/i586-linux-ld )

我调用 cmake,设置 TC_PATH 以及工具链文件:

~/CMakeTest/output $ cmake -DTC_PATH:PATH=/opt/toolchain -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../

看起来 cmake 正在多次调用工具链文件。前两次,TC_PATH 检查成功,但后来,在识别编译器后,它抛出错误:

--  TC_PATH IS defined. /opt/toolchain
-- TC_PATH IS defined. /opt/toolchain
-- The C compiler identification is GNU 4.9.1
-- The CXX compiler identification is GNU 4.9.1
-- Check for working C compiler: /opt/toolchain/usr/bin/i586-linux/i586-linux-gcc
CMake Error at /home/gnac/CMakeTest/toolchain.cmake:4 (message):
TC_PATH not defined.
Call Stack (most recent call first):
/home/gnac/CMakeTest/output/CMakeFiles/3.0.2/CMakeSystem.cmake:6 (include)
CMakeLists.txt:2 (project)

那么,除了在 shell 中设置永久环境变量之外,我如何通过命令行设置 TC_PATH 变量,以便它在执行 cmake generate 命令时保留在上下文中?

最佳答案

在编译测试项目时,CMake默认不向其传递变量。

CMAKE_TRY_COMPILE_PLATFORM_VARIABLES将变量传递到测试项目的选项。为了解决您的问题,应将此行放入工具链文件中:

set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES TC_PATH)

关于c++ - CMake 命令行定义不会延续到工具链文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37356957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com