通过配置选项将 CMake 与 icc 结合使用的推荐方法?
我想将 Intel 编译器 icc (或 icpc)与基于 CMake 的项目一起使用(在 Linux 上)。我当然可以在调用 cmake 时导出 CXX 变量,例如
CXX=icpc cmake ../
,这样效果很好。不过,我想通过自定义选项提供此选择。为此,我解析自定义选项,例如
cmake -DMY_COMPILER_OPTION=Intel ..
as
IF (MY_COMPILER_OPTION STREQUAL "Intel")
MESSAGE(STATUS "** Compiling with Intel settings **")
SET(CMAKE_CXX_COMPILER "icpc")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -w")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
ENDIF ()
并将 CMAKE_CXX_COMPILER 与一些编译器标志一起设置。这也有效,但是有一个重要的“但是”。
我还想在使用 icc 编译时对我的代码使用选项 -ipo
(过程间优化),并且我需要在构建过程中编译静态库。为此,我需要使用英特尔的 xiar
(我猜还有 xilink
)。
cmake 实际上为此提供了一个特殊的属性,
set_property(TARGET mytarget PROPERTY INTERPROCEDURAL_OPTIMIZATION 1)
但这似乎只有在通过环境变量设置编译器时才能正常工作(然后使用 xar)。通过 CMAKE_CXX_COMPILER 设置编译器时,此属性将被忽略。
还有其他方法可以做到这一点吗?有什么推荐的方法吗?或者至少有一个解决方法?
I would like to use the Intel compiler icc (or icpc) with a CMake-based project (on Linux for what it's worth). I can of course export the CXX variable when calling cmake, e.g. like
CXX=icpc cmake ../
and this works fine. I would however like to make this choice available via a custom option. For this I parse custom option, e.g.
cmake -DMY_COMPILER_OPTION=Intel ..
as
IF (MY_COMPILER_OPTION STREQUAL "Intel")
MESSAGE(STATUS "** Compiling with Intel settings **")
SET(CMAKE_CXX_COMPILER "icpc")
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -w")
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
ENDIF ()
and set CMAKE_CXX_COMPILER
together with some compiler flags. This also works, however there is an important "but".
I would also like to use the option -ipo
(interprocedural optimization) for my code when compiling with icc plus I need to compile a static library within the build process. For this to work, I need to use Intel's xiar
(and also xilink
I guess).
cmake actually offers a special property for this
set_property(TARGET mytarget PROPERTY INTERPROCEDURAL_OPTIMIZATION 1)
however this only seems to works properly when the compiler has been set via the environment variable (then xiar is used). When setting the compiler via CMAKE_CXX_COMPILER
this property is ignored.
Is there another way to do this?. Some recommended way? Or at least a work-around?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
推荐的方法是运行
cmake
使用适当的CMAKE__COMPILER
为每种语言定义。Intel oneAPI 编译器
建议使用 CMake 3.20 或更高版本,如
CMAKE_< LANG>_COMPILER_ID
是IntelLLVM
。较旧的 CMake 版本会将这些标识符解释为Clang
。Linux 用户
Windows 用户
从英特尔(r) oneAPI 工具控制台:
英特尔经典编译器
Linux 用户
Windows 用户
从英特尔编译器控制台:
The recommended way is to run
cmake
with the appropriateCMAKE_<LANG>_COMPILER
defined for each language.Intel oneAPI compilers
It's recommended to use CMake 3.20 or later, as
CMAKE_<LANG>_COMPILER_ID
isIntelLLVM
. Older CMake versions will interpret these identifiers asClang
.Linux users
Windows users
From an Intel(r) oneAPI Tools console:
Intel Classic compilers
Linux users
Windows users
From an Intel Compiler console:
好吧,由于这里没有答案,我也转向了 CMake 邮件列表 列出了有关此问题的帮助。那里的专家们似乎都同意我尝试这样做的方式是一个相当糟糕的主意。原因是我的自定义标志的解析发生在初始化过程的后期。因此,人们应该依靠通过环境变量设置编译器,并让 CMake 在初始配置运行期间发挥其魔力。我会修改我的方法..
Ok, since there have been no answers here, I've also turned to the CMake mailing list list for help on this issue. The consent of the experts there seems to be that the way I was trying to do this is a rather bad idea. The reason is that the parsing of my custom flags happens to late in the initialization process. One should therefore rely on setting the compiler via environment variables and let CMake do its magic during the initial configuration run. I will modify my approach..
基本上,避免尝试更改编译器。请参阅常见问题解答:如何做我使用不同的编译器?。 CMake 工具链文件提供了一种支持某些复杂情况的方法,例如奇异的 HPC 编译器。
Basically, avoid trying to change the compiler. See the FAQ: How do I use a different compiler?. CMake Toolchain files provide a way to support some complex cases, like an exotic HPC compiler.