通过配置选项将 CMake 与 icc 结合使用的推荐方法?

发布于 2025-01-01 16:20:23 字数 948 浏览 1 评论 0原文

我想将 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_COMPILERthis property is ignored.

Is there another way to do this?. Some recommended way? Or at least a work-around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染墨丶若流云 2025-01-08 16:20:23

推荐的方法是运行 cmake 使用适当的 CMAKE__COMPILER为每种语言定义。


Intel oneAPI 编译器

建议使用 CMake 3.20 或更高版本,如 CMAKE_< LANG>_COMPILER_IDIntelLLVM。较旧的 CMake 版本会将这些标识符解释为 Clang

Linux 用户

cmake -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DCMAKE_Fortran_COMPILER=ifx ..

Windows 用户

从英特尔(r) oneAPI 工具控制台:

cmake -GNinja -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icx -DCMAKE_Fortran_COMPILER=ifx ..

英特尔经典编译器

Linux 用户

cmake -DCMAKE_C_COMPILER=icc -DCMAKE_CXX_COMPILER=icpc -DCMAKE_Fortran_COMPILER=ifort ..

Windows 用户

从英特尔编译器控制台:

cmake -G "NMake Makefiles" -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_Fortran_COMPILER=ifort ..

The recommended way is to run cmake with the appropriate CMAKE_<LANG>_COMPILER defined for each language.


Intel oneAPI compilers

It's recommended to use CMake 3.20 or later, as CMAKE_<LANG>_COMPILER_ID is IntelLLVM. Older CMake versions will interpret these identifiers as Clang.

Linux users

cmake -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -DCMAKE_Fortran_COMPILER=ifx ..

Windows users

From an Intel(r) oneAPI Tools console:

cmake -GNinja -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icx -DCMAKE_Fortran_COMPILER=ifx ..

Intel Classic compilers

Linux users

cmake -DCMAKE_C_COMPILER=icc -DCMAKE_CXX_COMPILER=icpc -DCMAKE_Fortran_COMPILER=ifort ..

Windows users

From an Intel Compiler console:

cmake -G "NMake Makefiles" -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_Fortran_COMPILER=ifort ..
活雷疯 2025-01-08 16:20:23

好吧,由于这里没有答案,我也转向了 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..

梦在夏天 2025-01-08 16:20:23

基本上,避免尝试更改编译器。请参阅常见问题解答:如何做我使用不同的编译器?。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文