使用 native-CUDA-support CMake 获取 C++ 目标中的 CUDA 包含目录? [英] Obtaining the CUDA include dir in C++ targets with native-CUDA-support CMake?

查看:38
本文介绍了使用 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 包含目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆