'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?



Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 codecircuit