60

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?

clstaudt
  • 21,436
  • 45
  • 156
  • 239

7 Answers7

74

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 ..
Slava
  • 43,454
  • 1
  • 47
  • 90
  • 3
    Can you specify how to specify `CUDA_TOOLKIT_ROOT_DIR` to cmake? I am not usually a cmake user. – clstaudt Nov 14 '13 at 15:23
  • 1
    That got me one step further: `Could NOT find CUDA (missing: CUDA_NVCC_EXECUTABLE)`. Probably the installation is incomplete, since I have only libraries and headers, not executables. – clstaudt Nov 14 '13 at 15:27
  • 2
    @cls looks like it needs executable nvcc. Also most probably it could not find location of CUDA for the exactly same reason. After you install CUDA properly I bet you will not have to specify CUDA_TOOLKIT_ROOT_DIR manually. – Slava Nov 14 '13 at 15:30
  • You are right, after I installed the missing CUDA parts, cmake worked. `make` then fails with errors like: `/usr/local/cuda-5.5/include/thrust/system/cuda/detail/detail/launch_closure.inl(86): error: kernel launches from templates are not allowed in system files` Is this my configuration error or a source code error of the program author? – clstaudt Nov 14 '13 at 15:44
  • 1
    https://code.google.com/p/thrust/issues/detail?id=359 Try removing the variable CPLUS_INCLUDE_PATH – Slava Nov 14 '13 at 15:48
  • That worked. Another step further, but now I have this linker error: `Linking CXX executable clu /usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: CMakeFiles/clu.dir/clu.cpp.o: undefined reference to symbol 'clock_gettime@@GLIBC_2.2.5' /usr/lib64/gcc/x86_64-suse-linux/4.7/../../../../x86_64-suse-linux/bin/ld: note: 'clock_gettime@@GLIBC_2.2.5' is defined in DSO /lib64/librt.so.1 so try adding it to the linker command line` How can I add `-lrt` to the build process? – clstaudt Nov 14 '13 at 15:58
  • @cls target_link_libraries( rt) – Slava Nov 14 '13 at 16:04
  • Excuse my cmake ignorance, but I tried adding `target_link_libraries(clu rt)` to `CMakeLists` and this failed with `Attempt to add link library "rt" to target "clu" which is not built in this directory.` – clstaudt Nov 14 '13 at 16:12
  • I think you should open another question, this going to out of topic and you need to provide more details – Slava Nov 14 '13 at 16:19
  • Quick notice: Make sure you actually *install* the Nvidia Toolkit first. (The CUDA Driver, the Toolkit, and the Samples are each separate but can all be accessed through the installer) – joshfindit Nov 17 '17 at 02:01
24

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.

Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
  • 3
    this fixed it for me after two days of digging. for some reason installing cuda from apt-get didn't symlink /cuda with /cuda-8.0 – adam Jan 06 '17 at 14:58
  • I wonder why this good ol' answer with 15 upvotes is below the newer ones with just 3 and 5 upvotes.. – mirekphd Aug 06 '20 at 10:04
16

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)

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Kjell
  • 492
  • 1
  • 7
  • 15
7

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

Hopobcn
  • 885
  • 10
  • 20
  • 3
    That doesn't help with the original question: how do you let CMake know where to find CUDA? That still applies when using the new method. If anything, the new version seems significantly worse at automatically finding CUDA than FindCUDA! – Arthur Tacca Mar 11 '21 at 10:15
2

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.

Harshil Sharma
  • 2,016
  • 1
  • 29
  • 54
2

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.

simo23
  • 506
  • 4
  • 12
0

You can use enable_language(CUDA) as @Hopobcn suggested after setting project. You can then set the env variable CUDACXX to point to the nvcc of the version you want to use (f.e. export CUDACXX=/usr/local/cuda-11.8/bin/nvcc)

nomis6432
  • 236
  • 2
  • 10