SemtexB
SemtexB

Reputation: 660

How to link cusparse using CMakeLists.txt

How can I add the cusparse library from CUDA in a CMakeLists.txt-file, such that the nvcc compiler includes it automatically with -lcusparse? I already added the line

set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-lcusparse)

in CMakeLists.txt with no success. It looks like I'm missing something, because Nsight throws the error

undefined reference to 'cusparseDestroyMatDescr'.

Although when I exclude this line where cusparseDestroyMatDescr is called via commenting it, the Nsight project builds with no error, even with these three lines of code included

cusparseStatus_t status;
cusparseHandle_t handle=0;
cusparseMatDescr_t descr=0;

So it looks like it knows what cusparseStatus_t and so on is, but it does not know what cusparseDestroyMatDescr is.

What do I miss?

Upvotes: 0

Views: 1645

Answers (2)

codecircuit
codecircuit

Reputation: 121

I recommend to use the CMake CUDAToolkit package, which is available with CMake 3.17 and newer:

find_package(CUDAToolkit REQUIRED)
...
target_link_libraries(target CUDA::cusparse)

Upvotes: 0

havogt
havogt

Reputation: 2822

The correct way in CMake to link a library is using target_link_libraries( target library ).

If you use FindCUDA to locate the CUDA installation, the variable CUDA_cusparse_LIBRARY will be defined. Thus, all you need to do is

target_link_libraries( target ${CUDA_cusparse_LIBRARY} )

Upvotes: 3

Related Questions