You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译OpenCV 3.4.16+CUDA 12时遇未定义标识符错误求助

编译带CUDA的OpenCV报错解决

问题描述

尝试编译带CUDA的OpenCV时出现编译错误,具体日志如下:

[  7%] Building CXX object 3rdparty/protobuf/CMakeFiles/libprotobuf.dir/src/google/protobuf/stubs/atomicops_internals_x86_gcc.cc.o
/home/totar/cv2/opencv-3.4.16/modules/cudev/include/opencv2/cudev/ptr2d/texture.hpp(61): error: texture is not a template

/home/totar/cv2/opencv-3.4.16/modules/cudev/include/opencv2/cudev/ptr2d/texture.hpp(83): error: identifier "cudaUnbindTexture" is undefined

/home/totar/cv2/opencv-3.4.16/modules/core/include/opencv2/core/cuda/common.hpp(99): error: identifier "textureReference" is undefined

3 errors detected in the compilation of "/home/totar/cv2/opencv-3.4.16/modules/core/src/cuda/gpu_mat.cu".
CMake Error at cuda_compile_1_generated_gpu_mat.cu.o.Release.cmake:279 (message):
  Error generating file
  /home/totar/cv2/opencv-3.4.16/build/modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_gpu_mat.cu.o


modules/core/CMakeFiles/opencv_core.dir/build.make:63: recipe for target 'modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_gpu_mat.cu.o' failed
make[2]: *** [modules/core/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_gpu_mat.cu.o] Error 1
CMakeFiles/Makefile2:1889: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

使用环境:

  • nvidia-driver-525.60.13
  • CUDA 12.0
  • OpenCV 3.4.16

nvcc版本输出:

totar@totar:~/cv2/opencv-3.4.16/build$ nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Mon_Oct_24_19:12:58_PDT_2022
Cuda compilation tools, release 12.0, V12.0.76
Build cuda_12.0.r12.0/compiler.31968024_0

错误原因

OpenCV 3.4.16的CUDA模块依赖CUDA旧版API,而CUDA 12.0已移除texture模板、cudaUnbindTexture、textureReference这些接口,导致版本不兼容,编译失败。

解决方案

方案1:降级CUDA版本

将CUDA降级到OpenCV 3.4.16支持的版本,推荐CUDA 11.4或CUDA 10.2,这两个版本与OpenCV 3.4.x系列兼容性较好。

方案2:升级OpenCV版本

升级到OpenCV 4.5.5及以上版本,这些版本已适配CUDA 12.0的API变更,可正常编译。

方案3:手动修改OpenCV源码(不推荐)

若必须保留当前版本组合,需手动修改OpenCV的CUDA相关源码,用CUDA 12.0支持的纹理对象(texture object)替换旧的纹理引用接口。但该操作需熟悉CUDA API与OpenCV源码结构,修改量较大,易引入新问题。

内容的提问来源于stack exchange,提问作者Thomas Saison

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 10:20:23