CMake 无法正确找到 CUDA 库

新手上路,请多包涵

我正在尝试构建一个需要 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 呢?

原文由 clstaudt 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

在您现在发布的存档中,有多个项目层次结构。您在问题中发布的实际错误是在编译和链接基于 clutbb/cluster/bin 目录中 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 的路径,但可能没有必要,具体取决于您的机器环境的设置方式。

原文由 Robert Crovella 发布,翻译遵循 CC BY-SA 3.0 许可协议

带有主机调用的 C++ 文件不知道它需要链接到 libcudart 。您必须为该文件所在的文件/二进制文件显式设置依赖项,例如。

 target_link_libraries(clu ${CUDA_LIBRARIES})

上面的回复略有错误。无论出于何种原因,似乎 libcuda.so 都安装在了意外的位置。您可以尝试将 CMAKE_LIBRARY_PATH 或/和 CUDA_LIB_PATH 设置为该路径。

我认为 CUDA_LIB_PATH 需要在 cmake 之外设置,例如 export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/

原文由 Anycorn 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题