Obtaining the CUDA include dir in C++ targets with native-CUDA-support CMake?

前端 未结 1 1159
遇见更好的自我
遇见更好的自我 2020-12-19 13:24

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 locat

1条回答
  •  天涯浪人
    2020-12-19 14:10

    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.

    0 讨论(0)
提交回复
热议问题