opencv源码用cuda编译生成多重定义link错误

Opencv source compile with cuda generates multiple definition link error

我想用 cuda 8 构建 opencv 3.2,我已经在我的 linux 系统中安装了 cuda 所需的所有库,这是一个 Debian 9 testing (stretch) 4.9.0-1-amd64 使用 cmake 成功生成 makefile 后,通过执行 make 命令出现多重定义链接错误。 我用 cmake 尝试了很多配置,我也检查了很多论坛中几乎所有相关问题,但我找不到任何解决方案。 在 cmake 中,我将 cuda 编译器更改为 gcc-5,因为 cuda 8 需要 gcc 编译器不晚于版本 5,默认 gcc 编译器是版本 6.3 这是我的 cmake 配置输出:

OpenCV modules:
    To be built:                 cudev core cudaarithm flann imgproc ml video cudabgsegm cudafilters cudaimgproc cudawarping imgcodecs photo shape videoio cudacodec highgui objdetect ts features2d calib3d cudafeatures2d cudalegacy cudaobjdetect cudaoptflow cudastereo java stitching superres videostab python2 python3
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 viz

  GUI: 
    QT 5.x:                      YES (ver 5.7.1)
    QT OpenGL support:           YES (Qt5::OpenGL 5.7.1)
    OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so)
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
    JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
    WEBP:                        build (ver encoder: 0x020e)
    PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.28)
    TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.7)
    JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
    OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  YES (ver 2.2.5)
    FFMPEG:                      YES
      avcodec:                   YES (ver 57.64.101)
      avformat:                  YES (ver 57.56.101)
      avutil:                    YES (ver 55.34.101)
      swscale:                   YES (ver 4.2.100)
      avresample:                YES (ver 3.1.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/YES
    XIMEA:                       NO
    Xine:                        NO
    gPhoto2:                     YES

  Parallel framework:            TBB (ver 4.3 interface 8006)

  Other third-party libraries:
    Use IPP:                     9.0.1 [9.0.1]
         at:                     /home/user/opencv_build/3rdparty/ippicv/ippicv_lnx
    Use IPP Async:               NO
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  NO
    Use Eigen:                   YES (ver 3.3.2)
    Use Cuda:                    YES (ver 8.0)
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  NVIDIA CUDA
    Use CUFFT:                   YES
    Use CUBLAS:                  YES
    USE NVCUVID:                 NO
    NVIDIA GPU arch:             20 30 35 37 50 52 60 61
    NVIDIA PTX archs:
    Use fast math:               YES

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                /home/user/git/opencv/opencv/3rdparty/include/opencl/1.2
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.13)
    Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.13)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.0)
    packages path:               lib/python2.7/dist-packages

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.5.3)
    Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.3)
    numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.12.0)
    packages path:               lib/python3.5/dist-packages

  Python (for build):            /usr/bin/python2.7

  Java:
    ant:                         /usr/bin/ant (ver 1.9.8)
    JNI:                         /usr/lib/jvm/java-8-openjdk-amd64/include /usr/lib/jvm/java-8-openjdk-amd64/include/linux /usr/lib/jvm/java-8-openjdk-amd64/include
    Java wrappers:               YES
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     /usr/bin/doxygen (ver 1.8.13)

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              YES

  Install path:                  /usr/local

这里是 make 命令的输出

[ 10%] Built target libwebp
[ 10%] Built target opencv_cudev
[ 14%] Built target opencv_core
[ 14%] Linking CXX shared library ../../lib/libopencv_cudaarithm.so
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_absdiff_scalar.cu.o:(.bss+0xd090): multiple definition of `cv::cudev::color_cvt_detail::c_RGB2YUVCoeffs_f'
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_absdiff_mat.cu.o:(.bss+0xd090): first defined here
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_absdiff_scalar.cu.o:(.bss+0xd070): multiple definition of `cv::cudev::color_cvt_detail::c_RGB2YUVCoeffs_i'
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_absdiff_mat.cu.o:(.bss+0xd070): first defined here
.....
.....
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_transpose.cu.o:(.bss+0x0): multiple definition of `cv::cudev::color_cvt_detail::c_LabCbrtTab'
CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_absdiff_mat.cu.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/build.make:11685: recipe for target 'lib/libopencv_cudaarithm.so.3.2.0' failed
make[2]: *** [lib/libopencv_cudaarithm.so.3.2.0] Error 1
CMakeFiles/Makefile2:1204: recipe for target 'modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all' failed
make[1]: *** [modules/cudaarithm/CMakeFiles/opencv_cudaarithm.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

好的,终于找到了解决方法。 (顺便说一句,我正在使用 CMake GUI)。

您必须在 CMake-GUI 选项中取消选中 CUDA_SEPARABLE_COMPILATION

不幸的是,这只是一种解决方法,CUDA 可分离编译不能在 Linux 中使用。 (你必须为此使用 Windows)。我在 OpenCV 存储库的一个问题 (https://github.com/opencv/opencv/issues/5557) 中找到了这个。

PS:

我在使用 Qt5 构建 OpenCV 时遇到问题。如果你成功了,请在评论中通知我。谢谢!

我研究了很多解决方法,最后我想出了一个在 Debian 9 中使用 Cuda 8 构建 OpenCV 3.2 的解决方案。这个解决方案有很多未解决的原因,但它确实有效:

1-您需要 gcc 编译器版本 5.1 到 5.x。

2-你应该为 g++ 安装等效的编译器版本 (您不需要卸载较新的编译器版本)

3-OpenCV 本身应使用 gcc 5.x 进行编译,因此您应在 CMAKE 中手动选择 gcc 和 g++ 编译器

4-应从 CMAKE 中禁用单独的编译选项 感谢@InfinityChaos 提供此优惠

5-应从 CMAKE 中禁用预编译器选项(非常重要)

6-您可以启用 Qt、OpenGL 和 OpenCl 选项不会出现问题

7-您不需要标记 TBB 选项。