CMake 无法正确找到 CUDA 库
CMake does not properly find CUDA library
我正在尝试构建一个需要 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?
在您现在发布的存档中,有多个项目层次结构。 您在问题中发布的实际错误发生在编译和链接基于 clu 的 clu 项目期间.cpp在 clutbb/cluster/bin 目录中。
在同一目录中,有一个 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
的路径,但可能不是必需的,具体取决于计算机环境的设置方式。
在 CMake 3.17+ 上执行此操作的正确方法是使用 FindCUDAToolkit
模块,如下所示:
find_package(CUDAToolkit REQUIRED)
target_link_libraries(my_target PRIVATE CUDA::cudart CUDA::cuda_driver)
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})
这是根据官方来源改编的,这里。
带有主机调用的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/
- 如何使用CMake将C++编译为CUDA
- 链接器错误-在ubuntu中找不到CMake和Cuda 10.1的lcudart
- 使用Cmake进行简单的CUDA程序
- 编译/添加 cuda 代码到现有项目 (CMake)
- CMake + CUDA "invalid device function"即使使用正确的 SM 版本
- cmake cuda在Windows上单独的编译静态LIB链接错误,但在Ubuntu上没有
- CMAKE和CUDA的权限误差
- Clang 4.0.0 Cuda和Cmake最小警告的最佳实践
- 基于CMAKE的CUDA应用程序的构建失败 - 没有传递给链接器的文件
- 使用 CMake 为 CUDA 进行多配置/多平台构建
- 在CMake中链接CUDA库
- CMAKE重新运行由于CUDA生成的文件
- 为什么CMAKE add_依赖项不适用于使用CUDA代码的库
- CMAKE中断openCV建筑物具有CUDA支持而没有错误
- 如何设置CMake以便与Visual Studio 2017RC和CUDA 8.0一起使用
- 使用CMAKE编译CUDA C++-指定了多个编译阶段
- 带有 CUDA 的 CMake:用不必要的引号 (\) 括起来的 ccbin
- 如何让cmake找到CUDA
- CMake + Cuda:在 Cuda-mode (--x=cu) 下编译 cpp 文件
- 未定义的引用 CUDA 和 CMAKE