如何使用 CMake 3.15 及更高版本查找并链接 CUDA 库?

App*_*per 4 c++ cuda cmake

我在类 Unix 系统上使用 CMake 3.15-rc3。

\n

我需要将我正在构建的程序与多个 CUDA 库链接,包括cublas, cufft, cusolver, curand, nppicc, nppial, nppist, nppidei, nppig, nppitc, npps

\n

根据我在网上找到的信息,我需要做这样的事情:

\n
add_executable(test benchmark.cpp)\nfind_package(CUDALibs)\ntarget_link_libraries(test CUDA::cudart CUDA::cublas CUDA::cufft CUDA::cusolver CUDA::curand CUDA::nppicc CUDA::nppial CUDA::nppist CUDA::nppidei CUDA::nppig CUDA::nppitc CUDA::npps)\n
Run Code Online (Sandbox Code Playgroud)\n

当我运行时,make出现以下错误:

\n
CMake Warning at CMakeLists.txt:27 (find_package):\n  By not providing "FindCUDALibs.cmake" in CMAKE_MODULE_PATH this project has\n  asked CMake to find a package configuration file provided by "CUDALibs",\n  but CMake did not find one.\n\n  Could not find a package configuration file provided by "CUDALibs" with any\n  of the following names:\n\n    CUDALibsConfig.cmake\n    cudalibs-config.cmake\n\n  Add the installation prefix of "CUDALibs" to CMAKE_PREFIX_PATH or set\n  "CUDALibs_DIR" to a directory containing one of the above files.  If\n  "CUDALibs" provides a separate development package or SDK, be sure it has\n  been installed.\n\n\n
Run Code Online (Sandbox Code Playgroud)\n

所以看起来我需要一个CUDALibsConfig.cmake文件。我从哪里获取这个文件以及如何告诉 cmake 使用它?

\n

如果我使用以下内容,它会起作用:

\n
find_package(CUDA REQUIRED)\ntarget_link_libraries(run_benchmarks tf libmxnet.so ${CUDA_LIBRARIES} ${CUDA_cusparse_LIBRARY} ${CUDA_cublas_LIBRARY} ${CUDA_npp_LIBRARY})\n\n
Run Code Online (Sandbox Code Playgroud)\n

但据此 find_package(cuda)被弃用,所以我想学习正确的用法。

\n

编辑\n我尝试了其中一个回复中建议的内容。\n我添加到了CUDA项目中LANGUAGES

\n
project(\n        test_project\n        DESCRIPTION "Test project"\n        LANGUAGES CXX CUDA\n        )\n
Run Code Online (Sandbox Code Playgroud)\n

然后我用了find_package( FindCUDAToolkit REQUIRED)

\n

但是,当我运行 cmake 时,出现以下错误:

\n
\xc2\xa0nchafni\xc2\xa0\xee\x82\xb0\xc2\xa0\xee\x82\xa0\xc2\xa0dev\xc2\xa0\xee\x82\xb0\xc2\xa0\xe2\x80\xa6\xc2\xa0\xee\x82\xb1\xc2\xa0sample_code\xc2\xa0\xee\x82\xb1\xc2\xa0benchmarks\xc2\xa0\xee\x82\xb1\xc2\xa0build\xc2\xa0\xee\x82\xb0\xc2\xa01\xc2\xa0\xee\x82\xb0\xc2\xa0cmake ..\n-- The CXX compiler identification is GNU 7.5.0\n-- The CUDA compiler identification is NVIDIA 10.1.243\n-- Check for working CXX compiler: /usr/bin/c++\n-- Check for working CXX compiler: /usr/bin/c++ -- works\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - done\n-- Detecting CXX compile features\n-- Detecting CXX compile features - done\n-- Check for working CUDA compiler: /usr/local/cuda-10.1/bin/nvcc\n-- Check for working CUDA compiler: /usr/local/cuda-10.1/bin/nvcc -- works\n-- Detecting CUDA compiler ABI info\n-- Detecting CUDA compiler ABI info - done\nCMake Error at CMakeLists.txt:17 (find_package):\n  By not providing "FindFindCUDAToolkit.cmake" in CMAKE_MODULE_PATH this\n  project has asked CMake to find a package configuration file provided by\n  "FindCUDAToolkit", but CMake did not find one.\n\n  Could not find a package configuration file provided by "FindCUDAToolkit"\n  with any of the following names:\n\n    FindCUDAToolkitConfig.cmake\n    findcudatoolkit-config.cmake\n\n  Add the installation prefix of "FindCUDAToolkit" to CMAKE_PREFIX_PATH or\n  set "FindCUDAToolkit_DIR" to a directory containing one of the above files.\n  If "FindCUDAToolkit" provides a separate development package or SDK, be\n  sure it has been installed.\n\n\n-- Configuring incomplete, errors occurred!\n\n
Run Code Online (Sandbox Code Playgroud)\n

我缺少什么?

\n

ein*_*ica 5

find_package(CUDA)对于用 CUDA 编写/使用 CUDA 编译器(例如 NVCC)编译的程序,不推荐使用。文档页面(强调我的):

不再需要使用该模块或调用find_package(CUDA) 编译CUDA代码。相反,应在命令的顶级调用中指定的语言中列出 CUDA project(),或 enable_language()使用 CUDA 调用命令。然后可以.cu通过调用add_library()和 直接将 CUDA() 源添加到程序中add_executable()

但从find_package(CUDA)CMake 版本 3.15 开始,对于仅使用启用 CUDA/CUDA 捆绑/CUDA 利用库的 C++ 代码,并没有真正弃用。

在 CMake 3.17 中,引入了一个新的宏/命令:(FindCUDAToolkit()并且这个,find_package(CUDAToolkit)。您不能将其与您的 CMake 版本一起使用; find_package(CUDA)即使它有点笨拙和过时,也可以很好地使用。

编辑:实际上升级到较新的 CMake 版本非常容易:KitWare 提供具有很少依赖性的二进制版本。在 Linux 系统上它们将是:

    linux-vdso.so.1
    libdl.so.2
    librt.so.1
    libpthread.so.0
    libm.so.6
    libc.so.6
    /lib64/ld-linux-x86-64.so.2
Run Code Online (Sandbox Code Playgroud)

...您将很难找到没有这些的系统。此外,即使安装在任意路径下,CMake 也能够区分其共享文件版本和 CMake 使用的系统版本。所以 - 没有理由坚持使用旧版本。