linker error /usr/bin/ld: cannot find -lcudart

Dear all,

I’m new in cuda world.
I’ve got a Tesla2050 NVIDIA card, installed under Linux (Ubuntu).
I installed the driver, sdk and tool kit. I made the tests (by compiling the toolkit) and everything seems ok.

I now want to compile my first own program. To do so, I use cmake to produce a structure of a future project.
Folders are organised this way:

myproject -> src -> core (contain the main c++ files)
-> cuda (contain the .cu files)
-> bin
-> doc

in the root folder (myproject) the CMakeLists.txt contains:
cmake_minimum_required(VERSION 2.8)

in src folder the CMakeLists.txt contains:
find_package(CUDA REQUIRED)

in core folder the CMakeLists.txt contains:

in cuda folder the CMakeLists.txt contains:

My program is just a test wish aim to execute a cosine on the device and get the result
After a cmake . in the myproject folder, I tape make and I get:

[ 50%] Building NVCC (Device) object src/cuda/./
– Removing myproject/src/cuda/./
/usr/bin/cmake -E remove myproject/src/cuda/./
– Generating dependency file: myproject/src/cuda/CMakeFiles/
/usr/local/cuda/bin/nvcc -M -D__CUDACC__ myproject/src/cuda/ -o myproject/src/cuda/CMakeFiles/ -m64 -Xcompiler ,"-g" -DNVCC -I/usr/local/cuda/include -I/usr/local/cuda/include
– Generating temporary cmake readable file: myproject/src/cuda/CMakeFiles/
/usr/bin/cmake -D input_file:FILEPATH=myproject/src/cuda/CMakeFiles/ -D output_file:FILEPATH=myproject/src/cuda/CMakeFiles/ -P /usr/share/cmake-2.8/Modules/FindCUDA/make2cmake.cmake
– Copy if different myproject/src/cuda/CMakeFiles/ to myproject/src/cuda/CMakeFiles/
/usr/bin/cmake -E copy_if_different myproject/src/cuda/CMakeFiles/ myproject/src/cuda/CMakeFiles/
– Removing myproject/src/cuda/CMakeFiles/ and myproject/src/cuda/CMakeFiles/
/usr/bin/cmake -E remove myproject/src/cuda/CMakeFiles/ myproject/src/cuda/CMakeFiles/
– Generating myproject/src/cuda/./
/usr/local/cuda/bin/nvcc myproject/src/cuda/ -c -o myproject/src/cuda/./ -m64 -Xcompiler ,"-g" -DNVCC -I/usr/local/cuda/include -I/usr/local/cuda/include
Generated myproject/src/cuda/./ successfully.
– Generating myproject/src/cuda/./
/usr/local/cuda/bin/nvcc myproject/src/cuda/ -m64 -Xcompiler ,"-g" -DNVCC -cubin -o myproject/src/cuda/./ -I/usr/local/cuda/include -I/usr/local/cuda/include
– Executing the parser script
/usr/bin/cmake -D input_file:STRING=myproject/src/cuda/./ -P /usr/share/cmake-2.8/Modules/FindCUDA/parse_cubin.cmake
Linking CXX static library libcuda.a
[ 50%] Built target cuda
[100%] Building CXX object src/core/CMakeFiles/myproject.dir/main.cpp.o
Linking CXX executable bin/myproject
/usr/bin/ld: cannot find -lcudart
collect2: ld returned 1 exit status
make[2]: *** [src/core/bin/semoul] Erreur 1
make[1]: *** [src/core/CMakeFiles/myprojectl.dir/all] Erreur 2
make: *** [all] Erreur 2

I tried many think without success. Does anybody have any idear on what’s wrong.


Just a quick check. Do you have the path to in your LD_LIBRARY_PATH (it needs to be a global varible in your shell)?

e.g. In my .bashrc file I have…

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64

I don’t actually use cmake (I just use GNU make. There is a simple example of a makefile in the nvcc documentation) so I don’t think I will be of much help.

Thanks for your answer. I forgot to precise that axpect.

I configured the LD_LIBRARY_PATH at installation time and I checked that it contains the right path to the library.

But it still do not compiled.

Have you checked that you are able to compile the program manually without using cmake? I.e.

#compile object (.o) files

nvcc -c -o main.o

nvcc -c -o something.o

# etc...

#Now try to link

nvcc main.o something.o [other objects ..] -o program

#If the link fails try to manually add directory to search for libraries

nvcc -L/usr/local/cuda/lib64/ main.o something.o [other objects ..] -o program

You should note the extension of the source file is important. .cpp is interpreted as c++ and .cu is interpreted as a CUDA source file. I would advise having a read of the nvcc manual if you’re still having problems.

This error tells you exactly what is going wrong. The linker can’t find the cudart library. Setting the LD_LIBRARY_PATH is a run-time and not compile-time resolution for finding libraries. Now, I’ll explain how to get CMake to find cudart properly.

target_link_libraries( semoul




target_link_libraries() can handle several types of libraries. The first is if the library is a target within the CMake project. There are two libraries you with to link against semoul. When CMake sees ‘cuda’ it will try and find a target with the same name in your project. In your other directory you have created a library called cuda ( CUDA_add_library(cuda ). CMake will then link the cuda library with semoul. The next library is cudart. CMake checks its list of targets and doesn’t find one. It then checks to see if cudart is a full path to a library (which it isn’t). At this point CMake assumes that you know what you are doing and adds -lcudart to the link line.

The best way to handle linking external libraries is to use the output of the find_library() command. This is what FindCUDA.cmake does in the CMake distribution. If you look at the documentation, you will notice that there are several variables that FindCUDA produces. One of these is the CUDA_LIBRARIES variables that contain all the libraries needed to link a cuda run time application. These libraries are automatically added with CUDA_ADD_LIBRARY and CUDA_ADD_EXECUTABLE. What remains is adding it to your regular executable:

target_link_libraries( semoul




Try that and let me know.

Alternatively, you could have also added the path to cudart in the link command (see link_directories() documentation). This command adds the -L path to your link line. Note that this method isn’t as favored as the one I originally proposed.

Dear JBigler,

that was it.

I made the change and it compile find now.

Thank you very much for that help and further more for your clear explanations.

kind regards