C++ 如何让 cmake 找到 CUDA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19980412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to let cmake find CUDA
提问by clstaudt
I am trying to build this project, which has CUDA as a dependency. But the cmake script cannot find the CUDA installation on the system:
我正在尝试构建这个项目,它具有 CUDA 作为依赖项。但是cmake脚本在系统上找不到CUDA安装:
cls ~/workspace/gpucluster/cluster/build $ cmake ..
-- The C compiler identification is GNU 4.7.1
-- The CXX compiler identification is GNU 4.7.1
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at /usr/share/cmake/Modules/FindCUDA.cmake:488 (message):
Specify CUDA_TOOLKIT_ROOT_DIR
Call Stack (most recent call first):
CMakeLists.txt:20 (find_package)
-- Configuring incomplete, errors occurred!
-- 配置不完整,出现错误!
I've tried adding it as an environment variable to .bashrc
, to no effect:
我试过将它作为环境变量添加到.bashrc
,但没有效果:
export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-5.5
How do I Specify CUDA_TOOLKIT_ROOT_DIR
correctly?
我如何Specify CUDA_TOOLKIT_ROOT_DIR
正确?
回答by Slava
cmake mentioned CUDA_TOOLKIT_ROOT_DIR
as cmake variable, not environment one. That's why it does not work when you put it into .bashrc. If you look into FindCUDA.cmake it clearly says that:
cmake 被CUDA_TOOLKIT_ROOT_DIR
称为 cmake 变量,而不是环境变量。这就是为什么当您将其放入 .bashrc 时它不起作用的原因。如果您查看 FindCUDA.cmake,它会清楚地说明:
The script will prompt the user to specify CUDA_TOOLKIT_ROOT_DIR if the prefix cannot be determined by the location of nvcc in the system path and REQUIRED is specified to find_package(). To use a different installed version of the toolkit set the environment variable CUDA_BIN_PATH before running cmake (e.g. CUDA_BIN_PATH=/usr/local/cuda1.0 instead of the default /usr/local/cuda) or set CUDA_TOOLKIT_ROOT_DIR after configuring. If you change the value of CUDA_TOOLKIT_ROOT_DIR, various components that depend on the path will be relocated.
如果前缀无法由系统路径中 nvcc 的位置确定并且为 find_package() 指定了 REQUIRED,则脚本将提示用户指定 CUDA_TOOLKIT_ROOT_DIR。要使用工具包的不同安装版本,请在运行 cmake 之前设置环境变量 CUDA_BIN_PATH(例如 CUDA_BIN_PATH=/usr/local/cuda1.0 而不是默认的 /usr/local/cuda)或在配置后设置 CUDA_TOOLKIT_ROOT_DIR。如果更改 CUDA_TOOLKIT_ROOT_DIR 的值,则依赖于路径的各种组件将被重新定位。
So put CUDA_BIN_PATH
into .bashrc or specify CUDA_TOOLKIT_ROOT_DIR
to cmake:
因此放入CUDA_BIN_PATH
.bashrc 或指定CUDA_TOOLKIT_ROOT_DIR
为 cmake:
cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-5.5 ..
回答by Ashwin Nanjappa
FindCMake.cmake
looks for /usr/local/cuda
. In your case, that directory might not be there. Just create a symbolic link of that name to your actual CUDA installation directory:
FindCMake.cmake
寻找/usr/local/cuda
. 在您的情况下,该目录可能不存在。只需创建一个该名称的符号链接到您的实际 CUDA 安装目录:
$ sudo ln -s /usr/local/cuda-5.5 /usr/local/cuda
Your CMake should be able to generate the Makefile for your project now.
您的 CMake 现在应该能够为您的项目生成 Makefile。
回答by Hopobcn
Since CMake 3.8, FindCUDA is deprecated and the proper way of using CUDA in CMake projects is enabling it via project()
or enable_language()
从 CMake 3.8 开始,不推荐使用 FindCUDA,在 CMake 项目中使用 CUDA 的正确方法是通过project()
或enable_language()
回答by Kjell
Maybe cuda was installed from sources (and nvcc is not in the path). Then the script can not set CUDA_TOOLKIT_ROOT_DIR because of 'nvcc' missing. For me it worked fine after a 'sudo apt install nvidia-cuda-toolkit'.
也许 cuda 是从源代码安装的(并且 nvcc 不在路径中)。然后脚本无法设置 CUDA_TOOLKIT_ROOT_DIR 因为缺少 'nvcc'。对我来说,它在“sudo apt install nvidia-cuda-toolkit”之后工作正常。
回答by Harshil Sharma
In terminal, type nano ~/.bashrc
. Then add the following lines to the file:
在终端中,键入nano ~/.bashrc
。然后将以下行添加到文件中:
export PATH=$PATH:/usr/local/cuda/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib:/usr/local/lib
export CPLUS_INCLUDE_PATH=/usr/local/cuda/include
Save the file, then type source ~/.bashrc
in terminal.
保存文件,然后输入source ~/.bashrc
终端。
You may validate if CUDA path has been setup by typing nvcc --version
in terminal.
您可以通过nvcc --version
在终端中输入来验证是否已设置 CUDA 路径。