gpt4 book ai didi

cmake - 如何在cmake中自动查找编译器路径?

转载 作者:行者123 更新时间:2023-12-03 08:55:03 24 4
gpt4 key购买 nike

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

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

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 表示该路径不是现有编译器的完整路径,但如消息所示,它正是编译器所在的位置!

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

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

提前致谢。

最佳答案

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

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

详细说明

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

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

关于cmake - 如何在cmake中自动查找编译器路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56230175/

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