在 CMakeLists 中结合 C++ 和 CUDA 时将 CXX-standard 设置为 c++17

Set CXX-standard to c++17 when combining C++ and CUDA in CMakeLists(在 CMakeLists 中结合 C++ 和 CUDA 时将 CXX-standard 设置为 c++17)

本文介绍了在 CMakeLists 中结合 C++ 和 CUDA 时将 CXX-standard 设置为 c++17的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 CMake 的文档我只需要写

According to the documentation of CMake I just have to write

project(${PROJECT_NAME} LANGUAGES CUDA CXX)

当我想在一个项目中结合 CUDA 文件和本机 C++ 文件时.然后我不必再调用 cuda_add_executable(),而是调用 add_executable,CMake 应该自己解决所有问题.这很好用,除非我想为 C++ 代码指定一个标准(通过使用 set(CMAKE_CXX_STANDARD 17)).然后我收到错误消息

when I would like to combine CUDA-files and native C++-files in one project. Then I do not have to call cuda_add_executable() anymore, but rather add_executable, and CMake should figure out everything on its own. This works fine, unless I would like to specify a standard for C++-code (by using set(CMAKE_CXX_STANDARD 17)). Then I get the error message

Target requires the language dialect "CUDA17" (with compiler extensions), but CMake does not know the compile flags to use to enable it

是否有替代解决方案,或者我应该恢复到 find_package(CUDA)cuda_add_executable?

Is there an alternative solution, or should I rather revert to find_package(CUDA) and cuda_add_executable?

推荐答案

根据@talonmies 的评论,我找到了解决该问题的方法,方法是为每种语言显式设置变量,即 CUDACXX:

Based on the comment from @talonmies I found a solution for that problem by setting the variables explicitly for each language, i.e. CUDA and CXX:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

现在纯C++-文件按照C++17编译,CUDA-文件按照C++14编译.

Now the pure C++-files are compiled according to C++17, and the CUDA-files are compiled according to C++14.

这篇关于在 CMakeLists 中结合 C++ 和 CUDA 时将 CXX-standard 设置为 c++17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 CMakeLists 中结合 C++ 和 CUDA 时将 CXX-standard 设置为 c++17

基础教程推荐