一、出现问题
我在使用官方案例的时候,使用VS2022+CMake管理编译的时候出现如下的错误:
官方CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)set(ROUTINE bicgstab)project("${ROUTINE}_example"DESCRIPTION "GPU-Accelerated Sparse Linear Algebra"HOMEPAGE_URL "https://docs.nvidia.com/cuda/cusparse/index.html"LANGUAGES C CXX CUDA)find_package(CUDA REQUIRED)add_executable(${ROUTINE}_example)target_sources(${ROUTINE}_examplePUBLIC ${PROJECT_SOURCE_DIR}/${ROUTINE}_example.c
)target_include_directories(${ROUTINE}_examplePUBLIC ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)link_directories(${CUDA_LIB_PATH})
message(STATUS ${CUDA_LIB_PATH})
target_link_libraries(${ROUTINE}_examplePUBLIC cudart cusparse cublas
)
二、解决问题
开始我觉得是不是find_package(CUDA REQUIRED)
这里有一些bug,不能将CUDA一些库的目录正确设置,所以我又添加了:
link_directories("C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\lib\x64")
然后无效!
我在互联网上查找原因,发现可能是由于目录有空格导致的,需要在空格前使用转移字符:\
link_directories("C:\Program/ Files\NVIDIA/ GPU/ Computing/ Toolkit\CUDA\v12.3\lib\x64")
然后仍然无效!!
关键方案
然后查找官方文档:(有这么一段)链接
CUDA Runtime Library
The CUDA Runtime library (cudart) are what most applications will typically need to link against to make any calls such as cudaMalloc, and cudaFree.
Targets Created:
CUDA::cudart
CUDA::cudart_static
于是最后我的CMakeLists.txt改写成如下:
cmake_minimum_required(VERSION 3.9)set(ROUTINE bicgstab)project("${ROUTINE}_example"DESCRIPTION "GPU-Accelerated Sparse Linear Algebra"HOMEPAGE_URL "https://docs.nvidia.com/cuda/cusparse/index.html"LANGUAGES C CXX CUDA)find_package(CUDAToolkit REQUIRED)add_executable(${ROUTINE}_example)target_sources(${ROUTINE}_examplePUBLIC ${PROJECT_SOURCE_DIR}/${ROUTINE}_example.c
)target_include_directories(${ROUTINE}_examplePUBLIC ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
)link_directories(${CUDA_LIB_PATH})
message(STATUS ${CUDA_LIB_PATH})
target_link_libraries(${ROUTINE}_examplePUBLIC CUDA::cudart CUDA::cusparse CUDA::cublas
)
完成编译并运行!