How to let cmake find CUDA

C++BuildCudaCmake

C++ Problem Overview


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:

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:

export CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-5.5

How do I Specify CUDA_TOOLKIT_ROOT_DIR correctly?

C++ Solutions


Solution 1 - C++

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:

> 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.

So put CUDA_BIN_PATH into .bashrc or specify CUDA_TOOLKIT_ROOT_DIR to cmake:

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

Solution 2 - C++

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:

$ sudo ln -s /usr/local/cuda-5.5 /usr/local/cuda

Your CMake should be able to generate the Makefile for your project now.

Solution 3 - C++

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 running:

sudo apt install nvidia-cuda-toolkit

(This package might require several GiB of space)

Solution 4 - C++

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

https://cmake.org/cmake/help/v3.8/release/3.8.html#cuda

Solution 5 - C++

In terminal, type nano ~/.bashrc. Then add the following lines to the file:

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.

You may validate if CUDA path has been setup by typing nvcc --version in terminal.

Solution 6 - C++

In my case I had to make /usr/lib/cuda point to an actual CUDA installation. So I did:

ln -s /usr/lib/cuda /home/nvidia_libraries/cuda_10.2

I had cuda_10.2 installed locally in my home folder instead of system wide.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclstaudtView Question on Stackoverflow
Solution 1 - C++SlavaView Answer on Stackoverflow
Solution 2 - C++Ashwin NanjappaView Answer on Stackoverflow
Solution 3 - C++KjellView Answer on Stackoverflow
Solution 4 - C++HopobcnView Answer on Stackoverflow
Solution 5 - C++Harshil SharmaView Answer on Stackoverflow
Solution 6 - C++simo23View Answer on Stackoverflow