获得带有本地CUDA支持的CMake的C ++目标中的CUDA包含目录? [英] Obtaining the CUDA include dir in C++ targets with native-CUDA-support CMake?

查看:434
本文介绍了获得带有本地CUDA支持的CMake的C ++目标中的CUDA包含目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CMake 3.8版中,引入了对CUDA语言的原生支持。当项目将CUDA作为其语言之一时,CMake将继续查找CUDA(例如,它找到nvcc二进制文件)。



只要您仅编译CUDA代码-这就够了。但是,如果您想在该项目中编译C ++目标,该怎么办? CUDA包含不是自动 -I 编辑的,并且 CMakeCache.txt 似乎不包含CUDA包含路径



即使CMake本身已经定位,我是否还必须运行 find_package(需要CUDA 9.0)吗? CUDA?或者-我可以通过其他方式获取包含目录吗?

解决方案

包含目录可以从CMake变量CMAKE_CUDA_COMPILER 设置的编译器使用.html rel = nofollow noreferrer> CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES


用于获取,最好的方法可能是将 find_library() CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES


示例:

  cmake_minimum_required(版本3.9)
项目(MyProject版本1.0)
enable_language(CUDA)

find_library(CUDART_LIBRARY cudart $ {CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES })

add_exe cutable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp

target_include_directories(
binary_linking_to_cudart
PRIVATE
$ {CMAKE_CUDA_TOOLKIT_INCLUDE $ b target_link_libraries(
binary_linking_to_cudart
$ {CUDART_LIBRARY}

此问题是还对CMake错误跟踪器进行了讨论:为cuda库提供目标库。 / p>



更新:CMake 3.17.0添加了FindCUDAToolkit


代替了 find_library()手动操作,从CMake 3.17.0开始,最好的方法是使用 CUDAToolkit 模块。

  find_package(CUDAToolkit)
add_executable(
binary_linking_to_cudart
my_cpp_file_using_cudart.cpp

target_link_libraries(binary_linking_to_cudart CUDA :: c udart)

要获得较早版本CMake的支持,可以运送 CUDATookit 模块文件,只需在存储库中进行最少的更改。


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).

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.

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?

解决方案

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.

For getting the libraries, the best way is probably to use find_library() in combination with CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES.

Example:

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}
)

This issue is also discussed on the CMake bug tracker: Provide target libraries for cuda libraries.


Update: CMake 3.17.0 adds FindCUDAToolkit

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)

For support with earlier CMake versions, you can ship the CUDATookit module file with minimal changes in your repository.

这篇关于获得带有本地CUDA支持的CMake的C ++目标中的CUDA包含目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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