user3691191
user3691191

Reputation: 597

Linker error -lcudart not found with CMake and Cuda 10.1 in ubuntu

I am trying to build a very simple cuda example with CMake. The example calls the cudaGetDeviceCount function. I learnt from another post that I need to link it with the cudart library. I'm getting this error:

Found CUDA installation: /usr/local/cuda, version unknown
 "/usr/bin/ld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o testcuda /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../.. -L/usr/lib/llvm-6.0/bin/../lib -L/lib -L/usr/lib CMakeFiles/testcuda.dir/main.cpp.o -lcuda -lcudart -L/usr/local/cuda/lib64/stubs -L. -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/7.4.0/../../../x86_64-linux-gnu/crtn.o
/usr/bin/ld: cannot find -lcudart

Here is my CMakeLists.txt.

cmake_minimum_required(VERSION 3.15)
project (testcuda CXX CUDA)

if (NOT DEFINED CMAKE_CXX_STANDARD)
    set (CMAKE_CXX_STANDARD_17)
    set (CMAKE_CXX_STANDARD_REQUIRED True)
endif()

if (NOT DEFINED CMAKE_CUDA_STANDARD)
    set (CMAKE_CUDA_STANDARD_17)
    set (CMAKE_CUDA_STANDARD_REQUIRED True)
endif()

set (CMAKE_EXE_LINKER_FLAGS -v)

add_executable (testcuda main.cpp)
target_include_directories(testcuda PRIVATE ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
target_link_libraries(testcuda cuda cudart)
MESSAGE (STATUS ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
MESSAGE (STATUS ${CUDA_LIBRARIES})
MESSAGE (STATUS ${CUDA_VERSION})

The location of my libcudart is here:

/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudart.so

The output of variable CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES is /usr/local/cuda/targets/x86_64-linux/include, but both CUDA_LIBRARIES and CUDA_VERSION are empty.

My CMake is 3.15.

Have I missed anything?

Upvotes: 0

Views: 1046

Answers (1)

Michael
Michael

Reputation: 2414

When you use CUDA first-class language support with cmake it expects you to name any CUDA-relevant files with a .cu extension. Rename main.cpp to main.cu

Are you even using cudart? You shouldn't need it for a simple cudaGetDeviceCount call.

That said, that version unknown line is concerning. Normally /usr/local/cuda is a symbolic link, what does that point to? Check to make sure that it's pointing to cuda-10.1. If that's okay then you may want to reinstall the CUDA Toolkit

Upvotes: 1

Related Questions