CMake does not properly find CUDA library

后端 未结 3 801
终归单人心
终归单人心 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:23

    In the archive you have now posted, there are multiple project hierarchies. The actual error you have posted in the question is occurring during compile and linking of the clu project based on clu.cpp in the clutbb/cluster/bin directory.

    In this same directory, there is a CMakeLists.txt file. This file governs this particular level of the project hierarchy.

    In this particular CMakeLists.txt file, there is the following section:

    cuda_add_executable(clu clu.cpp)
    target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt)
    target_link_libraries(clu cluster)
    

    Try modifying the middle line above to:

    target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt cuda)
    

    This should fix the missing -lcuda in the linker command line. It may still be necessary to give it the path to libcuda.so on your machine, but it may not be necessary, depending on how your machine environment is set up.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-18 08:30

    The C++ file with host calls doesnt know it needs to link to libcudart. You have to explicitly set dependencies for the file/binary that file is in, eg.

    target_link_libraries(clu ${CUDA_LIBRARIES})
    

    The above response is slightly wrong. It looks like libcuda.so is installed in unexpected location for whatever reason. You can try setting CMAKE_LIBRARY_PATH or/and CUDA_LIB_PATH to that path.

    The CUDA_LIB_PATH needs to be set outside cmake I think, eg export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/

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