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

2022-01-10 00:00:00 cuda cmake c++

根据 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.

相关文章