gpt4 book ai didi

c++ - CMake 传递的变量编译器无法编译简单程序

转载 作者:太空宇宙 更新时间:2023-11-04 10:17:37 25 4
gpt4 key购买 nike

我正在尝试使用 CMake 交叉编译应用程序。当我对交叉编译器的路径进行硬编码时,一切正常。但是,当我通过命令行参数传递路径时,它似乎无法正确跟踪它们。

这是我的 shell 脚本,我从中克隆了一个存储库,应用了一个补丁,然后尝试进行 cmake 构建:

   cmake -D BASE=$PATH_TO_TOOLCHAIN_ROOT -D BOOST_ROOT=$BOOST_ROOT -D CMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../${GIT_PATH}/src/

这是我的 toolchain.cmake 文件:

set( CMAKE_SYSTEM_NAME Linux )
set( CMAKE_SYSTEM_VERSION 1 )
set(PKG_CONFIG_EXECUTABLE "/usr/bin/pkg-config")

message("Base: ${BASE}")
message("Boost: ${BOOST_ROOT}")

set(CMAKE_SYSROOT "${BASE}/sysroot")

set(CMAKE_C_COMPILER "${BASE}/sysroot/usr/bin/mipsel-linux-gcc")
set(CMAKE_CXX_COMPILER "${BASE}/sysroot/usr/bin/mipsel-linux-g++")

set( CMAKE_FIND_ROOT_PATH "${BASE}/sysroot")

set( CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
# for libraries and headers in the target directories
set( CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set( CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)


include_directories(${CMAKE_SYSROOT}/usr/local/include/)

set(BOOST_INCLUDEDIR ${BOOST_ROOT}/include)
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/lib)

include_directories(${BOOST_INCLUDEDIR})

set(OPENSSL_ROOT_DIR ${BASE}/sysroot/usr/lib/)
set(OPENSSL_SSL_LIBRARY libssl.so)
set(OPENSSL_CRYPTO_LIBRARY libcrypto.so)

同样,如果我将此工具链文件中的 ${BASE} 硬编码为/path/to/mipsel-linux-gcc,它就可以工作。

但是,当我实际调用它时,我得到了这个打印输出:

+ cmake -D BASE=/path/to/toolchain/root -D BOOST_ROOT=/home/matthew/boost/out -D CMAKE_TOOLCHAIN_FILE=../toolchain.cmake ../build/src/
Base: /path/to/toolchain/root
Boost: /home/matthew/boost/out
Base: /path/to/toolchain/root
Boost: /home/matthew/boost/out
-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc
Base:
Boost:
-- Check for working C compiler: /path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc -- broken
CMake Error at /usr/local/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler
"/path/to/toolchain/root/sysroot/usr/bin/mipsel-linux-gcc"
is not able to compile a simple test program.

It fails with the following output:

Change Dir: /home/matthew/Desktop/clones/build/CMakeFiles/CMakeTmp

在这里您可以清楚地看到它确实找到了编译器,并将它们都标识为 GNU 4.7.3。但是,请注意打印输出 Base:/path/to/toolchain/root。此 message() 指令仅在 toolchain.cmake 中列出一次,但它会在此处打印多次,并最终显示为空白变量。之后,它提示 C 编译器不工作,即使它随后清楚地打印出该编译器的完整路径。

我见过其他人的类似问题,但这似乎有所不同,因为当我将 toolchain.cmake 中的路径硬编码到 C 和 CXX 编译器时它工作正常。我尝试过在 C 和 CXX 编译器指令周围使用引号,也尝试过不使用引号。

我也有兴趣了解为什么我的消息指令打印了这么多次!

谢谢

最佳答案

CMake 正在进行测试构建,以确定编译器是否能够构建一个简单的应用程序(基本上只包含 main())。这可能会因多种原因而失败,例如,编译器无法使用默认的 CMake 检测标志等。

您应该检查 CMakeError.logCMakeOutput.log 以了解测试编译失败的原因。

要跳过编译器测试,不妨将以下内容放入工具链文件中:

set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

这将跳过编译器测试,但当然编译可能会在您实际构建项目时失败。

关于c++ - CMake 传递的变量编译器无法编译简单程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45382630/

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