CMake命令行定义不会导致工具链文件 [英] CMake Command Line Definitions Not Perpetuating To Toolchain File

查看:334
本文介绍了CMake命令行定义不会导致工具链文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a cmake cross compiler toolchain file, abridged as:

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以及工具链文件:

I call cmake, setting the TC_PATH as well as the toolchain file:

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

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

It appears cmake is invoking the toolchain file multiple times. On the first two time, the TC_PATH check succeeds, but later, after identifying the compilers, it throws an error:

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

So, outside of setting a permanent environment variable in the shell, how I can set the TC_PATH variable via the command line so that it will be remain in context while executing the cmake generate command?

推荐答案

您的工具链需要通过命令行来保存在上下文中自给自足。失败的步骤是 try_compile() 这不是你的缓存变量。

Your toolchain needs to be self-sufficient. The step that fails is a try_compile() which is not getting your cached variables.

您的工具链档案看起来不像是交叉编译(没有 CMAKE_SYSTEM_NAME ),因此您可以执行以下操作之一(除了设置 CC CXX 环境变数):

Your toolchain file does not look like you're cross-compiling (it does not have a CMAKE_SYSTEM_NAME) so you can do one of the following (besides setting the CC and CXX environment variables as you have mentioned):


  1. 足以给出 C 和/或 CXX 编译器的完整路径取决于您启用的语言),CMake将自动检测您的GNU工具链的其余部分。

  1. It's sufficient to give the full path to your C and/or CXX compiler (depending on which languages you enabled), CMake will detect the rest of your GNU toolchain automatically

cmake -DCMAKE_C_COMPILER:PATH=/opt/toolchain/usr/bin/i586-linux/i586-linux-gcc 
      -DCMAKE_CXX_COMPILER:PATH=/opt/toolchain/usr/bin/i586-linux/i586-linux-g++ ...


  • 将以下内容添加到您的工具链以跳过编译器测试(因为不是所有选项都可以传递给它,因为你选择的编译器/链接器不会产生有效的可执行文件)

  • Add the following to your toolchain to skip the compiler tests (because not all options may be passed to it or because your compiler/linker of choice will not produce a valid executable)

    set(CMAKE_C_COMPILER_WORKS 1 CACHE INTERNAL "")
    set(CMAKE_CXX_COMPILER_WORKS 1 CACHE INTERNAL "")
    

    或者只需使用 CMakeForceCompiler 宏,但使用的是Discouraged。如果可能,请避免使用此模块。

    Or just use the CMakeForceCompiler macros, but the use is "Discouraged. Avoid using this module if possible."

    使用 configure_file()文件(只需确保在 project()调用之前执行此操作)

    Use configure_file() to put the path into your toolchain file (just make sure to do it before the project() call)

    $ c> find_program()如果你的工具链的可能路径已知从外面设置它(见例如此处

    Prefer find_program() if the possible paths of your toolchain are known over setting it from the outside (see e.g. here)

    参考

    • CMake FAQ: How do I use a different compiler?
    • how to specify new gcc path for cmake
    • CMake Cross Compiling
    • cmake: problems specifying the compiler (2)
    • cmake cross-compile with specific linker doesn't pass arguments to armlink
    • CMake: In which Order are Files parsed (Cache, Toolchain, …)?

    这篇关于CMake命令行定义不会导致工具链文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆