加载libonnxruntime_providers_cuda.so报错找不到libcudnn.so.8如何解决?
解决ONNXRuntime加载CUDA Provider时找不到libcudnn.so.8的问题
报错信息
terminate called after throwing an instance of 'Ort::Exception' what(): /onnxruntime_src/onnxruntime/core/session/provider_bridge_ort.cc:1193 onnxruntime::Provider& onnxruntime::ProviderLibrary::Get() [ONNXRuntimeError] : 1 : FAIL : Failed to load library libonnxruntime_providers_cuda.so with error: libcudnn.so.8: cannot open shared object file: No such file or directory Signal: SIGABRT (Aborted)
当前CMakeLists.txt配置
file(GLOB cuda_libs /usr/local/cuda-11.8/lib64/*.so*) file(GLOB cudnn_libs /home/roroco/Downloads/cpp-lib/cudnn-linux-x86_64-8.9.0.131_cuda11-archive/lib/*.so.*) file(GLOB onnx_libs /home/roroco/Downloads/cpp-lib/onnxruntime-linux-x64-gpu-1.16.3/lib/*.so.*) file(GLOB cv_libs /home/roroco/Downloads/cpp-lib/opencv-4.9.0/dist/lib/*.so.*) set(libs ${cuda_libs} ${cudnn_libs} ${onnx_libs} ${cv_libs} /home/roroco/Downloads/cpp-lib/cudnn-linux-x86_64-8.9.0.131_cuda11-archive/lib/libcudnn.so.8 ) message("libs:${libs}") list(REMOVE_DUPLICATES libs) # Link the libraries to your executable target_link_libraries(inference ${libs})
解决步骤
1. 配置运行时动态库搜索路径
CMake的链接仅保证编译阶段找到库,运行时系统需要明确动态库的搜索路径,两种配置方式:
- 临时生效(当前终端):
export LD_LIBRARY_PATH=/home/roroco/Downloads/cpp-lib/cudnn-linux-x86_64-8.9.0.131_cuda11-archive/lib:$LD_LIBRARY_PATH
- 永久生效(所有终端):
编辑~/.bashrc(使用zsh则编辑~/.zshrc),添加:
export LD_LIBRARY_PATH=/home/roroco/Downloads/cpp-lib/cudnn-linux-x86_64-8.9.0.131_cuda11-archive/lib:/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH
执行source ~/.bashrc(对应你的shell配置文件)使配置生效。
2. 在CMake中设置RUNPATH(推荐)
直接在CMake中给可执行文件指定运行时库搜索路径,无需手动修改环境变量:
在target_link_libraries代码前添加:
# 设置运行时库搜索路径 set_target_properties(inference PROPERTIES BUILD_RPATH "/home/roroco/Downloads/cpp-lib/cudnn-linux-x86_64-8.9.0.131_cuda11-archive/lib;/usr/local/cuda-11.8/lib64;/home/roroco/Downloads/cpp-lib/onnxruntime-linux-x64-gpu-1.16.3/lib;/home/roroco/Downloads/cpp-lib/opencv-4.9.0/dist/lib" INSTALL_RPATH_USE_LINK_PATH TRUE )
编译后的可执行文件会自动到指定目录查找依赖库。
3. 验证库依赖状态
用ldd命令检查可执行文件的依赖是否能被找到:
ldd ./inference | grep cudnn
若输出中libcudnn.so.8显示not found,说明路径配置错误;若显示正确的文件路径,则配置有效。
4. 确认版本兼容性
你的ONNXRuntime 1.16.3、CUDA 11.8、CuDNN 8.9.0版本相互兼容,且CuDNN包名带cuda11,符合CUDA版本要求,无需调整版本。
内容的提问来源于stack exchange,提问作者chikadance
相关产品推荐
相关产品推荐

