CMake does not properly find CUDA library

后端 未结 3 804
终归单人心
终归单人心 2021-01-18 07:40

I\'m trying to build a program that requires CUDA. To the CMake script I supply:

cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..

CUDA is

3条回答
  •  囚心锁ツ
    2021-01-18 08:28

    The correct way of doing this on CMake 3.17+ is to use the FindCUDAToolkit module, like so:

    find_module(CUDAToolkit REQUIRED)
    target_link_libraries(my_target PRIVATE CUDA::cudart CUDA::cuda_driver)
    

    The CUDA::cuda_driver target is equivalent to -lcuda when the linker would find it, and is otherwise an absolute path to the correct library. You should avoid adding system libraries via target_link_libraries to ensure portability.


    If you have an older version (CMake 3.0+), you can use the (now-deprecated) FindCUDA module to imitate the 3.17 module, like so:

    find_package(CUDA REQUIRED)
    
    # Do what the new package does
    find_library(CUDA_DRIVER_LIBRARY
                 NAMES cuda_driver cuda
                 HINTS ${CUDA_TOOLKIT_ROOT_DIR}
                       ENV CUDA_PATH
                 PATH_SUFFIXES nvidia/current lib64 lib/x64 lib)
    if (NOT CUDA_DRIVER_LIBRARY)
        # Don't try any stub directories until we have exhausted all other search locations.
        find_library(CUDA_DRIVER_LIBRARY
                     NAMES cuda_driver cuda
                     HINTS ${CUDA_TOOLKIT_ROOT_DIR}
                           ENV CUDA_PATH
                     PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs)
    endif ()
    mark_as_advanced(CUDA_DRIVER_LIBRARY)
    ##
    
    target_include_directories(my_target PRIVATE ${CUDA_INCLUDE_DIRS})
    target_link_libraries(my_target PRIVATE ${CUDA_LIBRARIES} ${CUDA_DRIVER_LIBRARY})
    

    This is adapted from the official sources, here.

提交回复
热议问题