Obtaining the CUDA include dir in C++ targets with native-CUDA-support CMake?(使用 native-CUDA-support CMake 获取 C++ 目标中的 CUDA 包含目录?)
问题描述
在 CMake 版本 3.8 中,引入了对 CUDA 作为语言的原生支持.当项目将 CUDA 作为其语言之一时,CMake 将继续定位 CUDA(例如,它定位 nvcc 二进制文件).
In CMake version 3.8, native support for CUDA as a language was introduced. When a project has CUDA as one of its languages, CMake will proceed to locate CUDA (e.g. it locates the nvcc binary).
只要你只编译 CUDA 代码——这就足够了.但是如果你想在那个项目中编译一个 C++ 目标呢?CUDA 包含不是自动 -I
的,并且 CMakeCache.txt
似乎没有在任何地方包含 CUDA 包含路径.
As long as you only compile CUDA code - this is enough. But what if you want to compile a C++ target in that project? The CUDA includes are not -I
'ed automatically, and CMakeCache.txt
does not seem to contain the CUDA include path anywhere.
即使 CMake 本身已经找到了 CUDA,我是否真的需要运行一些东西 find_package(CUDA 9.0 REQUIRED)
?或者 - 我可以通过其他方式获取包含目录吗?
Do I actually have to run something find_package(CUDA 9.0 REQUIRED)
even when CMake itself has already located CUDA? Or - can I obtain the include directory some other way?
推荐答案
CMAKE_CUDA_COMPILER
设置的编译器使用的include目录可以从CMake 变量 CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
.
The include directories, which are used by the compiler set by CMAKE_CUDA_COMPILER
, can be retrieved from the CMake variable CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES
.
为了获得库,最好的方法可能是结合使用find_library()
和CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES
.
For getting the libraries, the best way is probably to use find_library()
in combination with CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES
.
例子:
cmake_minimum_required(VERSION 3.9)
project(MyProject VERSION 1.0)
enable_language(CUDA)
find_library(CUDART_LIBRARY cudart ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_include_directories(
binary_linking_to_cudart
PRIVATE
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)
target_link_libraries(
binary_linking_to_cudart
${CUDART_LIBRARY}
)
CMake 错误跟踪器也讨论了这个问题:为 cuda 库提供目标库.
This issue is also discussed on the CMake bug tracker: Provide target libraries for cuda libraries.
更新:CMake 3.17.0 添加 FindCUDAToolkit
从 CMake 3.17.0 开始,最好的方法是使用 CUDAToolkit
模块,而不是手动执行 find_library()
.
Instead of doing find_library()
manually, the best way as of CMake 3.17.0 would be to use the CUDAToolkit
module.
find_package(CUDAToolkit)
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp
)
target_link_libraries(binary_linking_to_cudart PRIVATE CUDA::cudart)
为了支持较早的 CMake 版本,您可以在您的存储库中提供 CUDATookit
模块文件,而只需对您的存储库进行最少的更改.
For support with earlier CMake versions, you can ship the CUDATookit
module file with minimal changes in your repository.
这篇关于使用 native-CUDA-support CMake 获取 C++ 目标中的 CUDA 包含目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 native-CUDA-support CMake 获取 C++ 目标中的 CUDA 包含目录?
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01