CMake不能正确找到CUDA库

问题描述 投票:6回答:3

我想建立一个需要CUDA的程序。对我提供的CMake脚本。

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

找到CUDA,CMake正常运行

staudt ~/workspace/clutbb/cluster/build $ cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..                                                                                  
-- Found CUDA: /usr/local/cuda (found version "6.5") 
-- Found Intel TBB
-- Boost version: 1.56.0
-- Found the following Boost libraries:
--   iostreams
--   program_options
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Could NOT find SDL (missing:  SDL_LIBRARY SDL_INCLUDE_DIR) 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/i11/staudt/workspace/clutbb/cluster/build

但是链接器的步骤却失败了

staudt ~/workspace/clutbb/cluster/build $ make
[ 69%] Built target cluster
Linking CXX executable clu
CMakeFiles/clu.dir/clu.cpp.o: In function `initCUDA(int&, CUctx_st*&, int const&)':
clu.cpp:(.text+0x517): undefined reference to `cuInit'
clu.cpp:(.text+0x52b): undefined reference to `cuDeviceGet'
clu.cpp:(.text+0x53f): undefined reference to `cuCtxCreate_v2'
clu.cpp:(.text+0x559): undefined reference to `cuDeviceGetName'
clu.cpp:(.text+0x55e): undefined reference to `cuCtxSynchronize'
CMakeFiles/clu.dir/clu.cpp.o: In function `exitCUDA(int&, CUctx_st*&)':
clu.cpp:(.text+0x684): undefined reference to `cuCtxDestroy_v2'
CMakeFiles/clu.dir/clu.cpp.o: In function `main':
clu.cpp:(.text.startup+0x1092): undefined reference to `cuCtxDestroy_v2'
clu.cpp:(.text.startup+0x10d1): undefined reference to `cuCtxSynchronize'
clu.cpp:(.text.startup+0x10e1): undefined reference to `cuCtxSynchronize'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/clu] Fehler 1
make[1]: *** [bin/CMakeFiles/clu.dir/all] Fehler 2
make: *** [all] Fehler 2

所需的库在... /usr/local/cuda/lib64/stubs/libcuda.so但我如何向cmake或make指出这一点?

c++ cuda makefile cmake
3个回答
5
投票

在你现在发布的存档中,有多个项目层次结构。 你在问题中发布的实际错误是发生在clutbbclusterbin目录下基于clu.cpp的clu项目的编译和链接过程中。

在这个目录下,有一个CMakeLists.txt文件。 这个文件管理着项目层次结构的这一特定层次。

在这个特殊的CMakeLists.txt文件中,有以下部分。

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

试着把上面中间的一行修改为:

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

这应该可以修正缺失的 -lcuda 命令行中的链接器。 可能还需要给它提供通往 libcuda.so 但可能没有必要,这取决于你的机器环境是如何设置的。


0
投票

带有主机调用的C++文件不知道需要链接到 libcudart. 你必须为该文件所在的filebinary明确设置依赖关系,例如。

target_link_libraries(clu ${CUDA_LIBRARIES})

上面的回答略有错误。 看起来像是libcuda.so由于某种原因被安装在了意外的位置。 你可以尝试设置 CMAKE_LIBRARY_PATHCUDA_LIB_PATH 到该路径。

CUDA_LIB_PATH 需要在 cmake 外部设置,例如 export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/


0
投票

在CMake 3.17+上,正确的方法是使用 FindCUDAToolkit 模块,像这样。

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

The CUDA::cuda_driver 相当于 -lcuda 当链接器会找到它的时候,否则就是指向正确库的绝对路径。你应该避免通过 target_link_libraries 以确保可移植性。


如果你有一个旧的版本(CMake 3.0+),你可以使用(现在已经过时的) FindCUDA 模块来模仿3.17模块,就像这样。

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

这是根据官方资料改编的,这里。

© www.soinside.com 2019 - 2024. All rights reserved.