【问题标题】:How to find compiler path automatically in cmake?如何在cmake中自动查找编译器路径?
【发布时间】:2019-10-07 08:51:40
【问题描述】:

我想在cmake中自动设置编译路径(例如:icc),这样我的程序只要安装了icc就可以在任何计算机上编译,我们不需要关心@在哪里987654324@已安装。

首先,我使用以下命令来设置编译器。一切正常。

set(Intel_C_COMPILER "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc")
set(Intel_CXX_COMPILER "/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc")

set(CMAKE_C_COMPILER   ${Intel_C_COMPILER}  )
set(CMAKE_CXX_COMPILER ${Intel_CXX_COMPILER})

project(MyProject)

....

然后,我想自动设置编译器路径,我知道下面的命令可以找到编译器路径

which icc

所以我写了以下命令尝试通过cmake自动设置编译器。

execute_process(COMMAND which icc  OUTPUT_VARIABLE Intel_C_COMPILER)
execute_process(COMMAND which icpc OUTPUT_VARIABLE Intel_CXX_COMPILER)

message(Intel_CXX_COMPILER: ${Intel_C_COMPILER})
message(Intel_CXX_COMPILER: ${Intel_CXX_COMPILER})

set(CMAKE_C_COMPILER   ${Intel_C_COMPILER}  )
set(CMAKE_CXX_COMPILER ${Intel_CXX_COMPILER})

project(MyProject)

....

在这种情况下,发生了一些奇怪的事情,cmake 显示:


 Intel_CXX_COMPILER:/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc

-- The C compiler identification is unknown
-- The CXX compiler identification is unknown

 CMake Error at CMakeLists.txt:27 (project):   The CMAKE_C_COMPILER:

    /opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc



  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the
environment   variable "CC" or the CMake cache entry CMAKE_C_COMPILER
to the full path to   the compiler, or to the compiler name if it is
in the PATH.


CMake Error at CMakeLists.txt:27 (project):   The CMAKE_CXX_COMPILER:

    /opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc



  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the
environment   variable "CXX" or the CMake cache entry
CMAKE_CXX_COMPILER to the full path   to the compiler, or to the
compiler name if it is in the PATH.


 -- Configuring incomplete, errors occurred!

CMake表示该路径不是现有编译器的完整路径,但如message所示,正是编译器所在的位置!

我知道我们可以设置编译器的其他技术,例如导出一些环境变量以帮助 cmake 找到路径,但我想知道为什么我的方法不起作用?

有没有更好的方法可以解决这个问题?

提前致谢。

【问题讨论】:

  • 在您的Intel_C_COMPILERIntel_CXX_COMPILER 中有traling 换行符:它是which <...> 命令输出的一部分,并被execute_process 抓取。要删除该换行符,请参阅该问题:stackoverflow.com/questions/39496043/…。顺便说一句,您是否将 CMake 日志正确格式化为 代码,您(和我们)很容易在您的路径中找到超出的换行符。这就是为什么日志应该避免格式化为 blockquote
  • @Tsyvarev 你直接暗示了这一点!非常感谢!我现在已将 CMake 日志编辑为代码。顺便说一句,您想复制您的评论作为答案吗?我想选择你的答案作为接受。

标签: cmake


【解决方案1】:

变量Intel_C_COMPILERIntel_CXX_COMPILER尾随换行符。该问题及其答案中描述了删除该换行符的方法:How to strip trailing whitespace in CMake variable?

例如,您可以使用 OUTPUT_STRIP_TRAILING_WHITESPACE 选项运行 execute_process,因此它的行为类似于 shell 的反引号运算符 (`which icc`)。

详细说明

大多数 shell 实用程序都输出带有尾随换行符的单(甚至多)行信息。实用程序which 也不例外。当在终端中运行这些实用程序时,带有尾随换行符的输出看起来不错。

但是当在脚本中运行这样的实用程序并以编程方式获取其输出时,需要关心这样的换行符。

【讨论】:

    【解决方案2】:

    一般来说,不能在项目中设置变量CMAKE_C_COMPILERCMAKE_CXX_COMPILER

    由于编译器检测发生在project() 调用中,因此必须在配置项目时尽早设置编译器。

    我建议您尝试以下方法:

    export CC=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc
    export CXX=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc
    
    cd /path/to/build
    cmake /path/to/src
    

    或者您也可以传递变量CMAKE_C_COMPILERCMAKE_CXX_COMPILER

    export CC=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc
    export CXX=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc
    
    cd /path/to/build
    cmake \
      -DCMAKE_C_COMPILER:FILEPATH=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icc \
      -DCMAKE_CXX_COMPILER:FILEPATH=/opt/intel/compilers_and_libraries_2019.0.117/linux/bin/intel64/icpc \
      /path/to/src
    

    重要提示:尝试这些命令时,请确保在空的构建目录中配置项目。

    【讨论】:

    • 实际上,我在命令project(...)之前设置了编译器,(我已经编辑了问题)。感谢您的回复,您的解决方案确实有效。但是我还是很困惑,如果编译器的路径是通过which icc获得的,为什么cmake找不到编译器。有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2018-05-25
    相关资源
    最近更新 更多