首页 > 解决方案 > 使用 OpenCV3.4.5 在 Qt5 中与 Cuda 相关的编译错误

问题描述

我使用 VS2015 和 Cuda8.0 构建 OpenCV3.4.5 并启用了 Cuda 支持(请参阅这篇文章How to Build OpenCV for Windows with CUDA)。整个构建过程顺利进行,构建:605 成功,1 失败,0 最新,0 跳过。供您参考,OpenCV3.4.5 的构建配置如下所示:

-- General configuration for OpenCV 3.4.5 =====================================
  --   Version control:               unknown
  --
  --   Platform:
  --     Timestamp:                   2019-01-09T23:07:28Z
  --     Host:                        Windows 10.0.17134 AMD64
  --     CMake:                       3.9.1
  --     CMake generator:             Visual Studio 14 2015 Win64
  --     CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
  --     MSVC:                        1900
  --
  --   CPU/HW features:
  --     Baseline:                    SSE SSE2 SSE3
  --       requested:                 SSE3
  --     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
  --       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
  --       SSE4_1 (6 files):          + SSSE3 SSE4_1
  --       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
  --       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
  --       AVX (6 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
  --       AVX2 (12 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
  --
  --   C/C++:
  --     Built as dynamic libs?:      YES
  --     C++11:                       YES
  --     C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24215.1)
  --     C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP4   /MD /O2 /Ob2 /DNDEBUG
  --     C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP4   /MDd /Zi /Ob0 /Od /RTC1
  --     C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
  --     C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP4    /MD /O2 /Ob2 /DNDEBUG
  --     C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP4  /MDd /Zi /Ob0 /Od /RTC1
  --     Linker flags (Release):      /machine:x64  /INCREMENTAL:NO
  --     Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
  --     ccache:                      NO
  --     Precompiled headers:         NO
  --     Extra dependencies:          cudart.lib nppc.lib nppial.lib nppicc.lib nppicom.lib nppidei.lib nppif.lib nppig.lib nppim.lib nppist.lib nppisu.lib nppitc.lib npps.lib cublas.lib cufft.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/lib/x64
  --     3rdparty dependencies:
  --
  --   OpenCV modules:
  --     To be built:                 calib3d core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev dnn features2d flann highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python3 python_bindings_generator shape stitching superres ts video videoio videostab world
  --     Disabled:                    -
  --     Disabled by dependency:      -
  --     Unavailable:                 java js python2 python2 viz
  --     Applications:                tests perf_tests examples apps
  --     Documentation:               NO
  --     Non-free algorithms:         NO
  --
  --   Windows RT support:            NO
  --
  --   GUI:
  --     QT:                          YES (ver 5.7.0)
  --       QT OpenGL support:         NO
  --     Win32 UI:                    YES
  --     VTK support:                 NO
  --
  --   Media I/O:
  --     ZLib:                        build (ver 1.2.11)
  --     JPEG:                        build-libjpeg-turbo (ver 1.5.3-62)
  --     WEBP:                        build (ver encoder: 0x020e)
  --     PNG:                         build (ver 1.6.35)
  --     TIFF:                        build (ver 42 - 4.0.9)
  --     JPEG 2000:                   build (ver 1.900.1)
  --     OpenEXR:                     build (ver 1.7.1)
  --     HDR:                         YES
  --     SUNRASTER:                   YES
  --     PXM:                         YES
  --
  --   Video I/O:
  --     DC1394:                      NO
  --     FFMPEG:                      YES (prebuilt binaries)
  --       avcodec:                   YES (ver 57.107.100)
  --       avformat:                  YES (ver 57.83.100)
  --       avutil:                    YES (ver 55.78.100)
  --       swscale:                   YES (ver 4.8.100)
  --       avresample:                YES (ver 3.7.0)
  --     GStreamer:                   NO
  --     DirectShow:                  YES
  --     Media Foundation:            YES
  --       DXVA:                      YES
  --
  --   Parallel framework:            Concurrency
  --
  --   Trace:                         YES (with Intel ITT)
  --
  --   Other third-party libraries:
  --     Intel IPP:                   2019.0.0 Gold [2019.0.0]
  --            at:                   E:/In-Field-Sorting-Pro/OpenCV/opencv-3.4.5/cmake-build/3rdparty/ippicv/ippicv_win/icv
  --     Intel IPP IW:                sources (2019.0.0)
  --               at:                E:/In-Field-Sorting-Pro/OpenCV/opencv-3.4.5/cmake-build/3rdparty/ippicv/ippicv_win/iw
  --     Lapack:                      NO
  --     Eigen:                       NO
  --     Custom HAL:                  NO
  --     Protobuf:                    build (3.5.1)
  --
  --   NVIDIA CUDA:                   YES (ver 8.0, CUFFT CUBLAS NVCUVID FAST_MATH)
  --     NVIDIA GPU arch:             20 30 35 37 50 52 60 61
  --     NVIDIA PTX archs:
  --
  --   OpenCL:                        YES (no extra features)
  --     Include path:                E:/In-Field-Sorting-Pro/OpenCV/opencv-3.4.5/sources/3rdparty/include/opencl/1.2
  --     Link libraries:              Dynamic load
  --
  --   Python 3:
  --     Interpreter:                 C:/Python35/python3.exe (ver 3.5.4)
  --     Libraries:                   C:/Python35/libs/python35.lib (ver 3.5.4)
  --     numpy:                       C:/Python35/lib/site-packages/numpy/core/include (ver 1.13.1)
  --     install path:                C:/Python35/Lib/site-packages/cv2/python-3.5
  --
  --   Python (for build):            C:/Python35/python3.exe
  --
  --   Java:
  --     ant:                         NO
  --     JNI:                         NO
  --     Java wrappers:               NO
  --     Java tests:                  NO
  --
  --   Install to:                    C:/opencv-3.4.5/mybuild

使用Qt5.7和自建的OpenCV3.4.5,编译我的Qt项目会导致数百个cuda相关的错误,其中一部分(例如,`C2061:语法错误:标识符)如下所示:

c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): warning C4002: too many actual parameters for macro 'min'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2182: 'InputArray': illegal use of type 'void'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2146: syntax error: missing ';' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2059: syntax error: ')'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2143: syntax error: missing ';' before '<'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2146: syntax error: missing ')' before identifier 'src2'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2086: 'int cv::cuda::InputArray': redefinition
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): note: see declaration of 'cv::cuda::InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): error C2146: syntax error: missing ';' before identifier 'src2'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): warning C4002: too many actual parameters for macro 'max'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2182: 'InputArray': illegal use of type 'void'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2086: 'int cv::cuda::InputArray': redefinition
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): note: see declaration of 'cv::cuda::InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2146: syntax error: missing ';' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2059: syntax error: ')'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2086: 'int cv::cuda::src1': redefinition
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(296): note: see declaration of 'cv::cuda::src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2059: syntax error: '>'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2146: syntax error: missing ')' before identifier 'src2'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(307): error C2146: syntax error: missing ';' before identifier 'src2'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(330): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(334): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(335): error C2143: syntax error: missing ';' before '{'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(335): error C2447: '{': missing function header (old-style formal list?)
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(351): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(361): error C2146: syntax error: missing ')' before identifier 'xy'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(369): error C2146: syntax error: missing ')' before identifier 'xy'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(379): error C2146: syntax error: missing ')' before identifier 'x'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(389): error C2146: syntax error: missing ')' before identifier 'x'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(401): error C2146: syntax error: missing ')' before identifier 'x'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(414): error C2146: syntax error: missing ')' before identifier 'x'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(425): error C2146: syntax error: missing ')' before identifier 'magnitude'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(453): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(455): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(465): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(480): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(494): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(501): error C2146: syntax error: missing ')' before identifier 'lut'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(518): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(534): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(536): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(546): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(548): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(557): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(559): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(566): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(568): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(575): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(577): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(590): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(592): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(607): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(610): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(621): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(623): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(651): error C2146: syntax error: missing ')' before identifier 'mtx'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(661): error C2146: syntax error: missing ')' before identifier 'mtx'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(663): error C2146: syntax error: missing ')' before identifier 'mtx'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(673): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(691): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(703): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(712): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(744): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(761): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(778): error C2146: syntax error: missing ')' before identifier 'src1'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(815): error C2146: syntax error: missing ')' before identifier 'src'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(828): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudaarithm.hpp(861): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudabgsegm.hpp(85): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudabgsegm.hpp(132): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafilters.hpp(84): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafilters.hpp(120): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafilters.hpp(159): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafilters.hpp(261): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafeatures2d.hpp(154): error C2061: syntax error: identifier 'InputArray'
c:\opencv-3.4.5\mybuild\include\opencv2/cudafeatures2d.hpp(160): error C2061: syntax error: identifier 'InputArray'

值得注意的是,我的项目可以在没有 Cuda 支持的情况下使用官方预构建的 OpenCV3.4.5 正常工作!所以这些错误一定是因为启用了 cuda 的 opencv 构建。

令我困惑的是,我的项目没有明确涉及 cuda 计算,而且我也没有包含任何与 cuda 相关的头文件(我的 OpenCV 构建生成单个opencv_world345文件)。对这些错误的一种可能猜测是不适当的包含标头,例如#include <windows.h>.

有没有人遇到过类似的错误?如果我想保留 cuda 功能,而不是使用预构建版本,如何解决它们?

标签: qtopencv

解决方案


我遇到过类似的问题,我可以找到解决方法。就我而言,我正在同一个项目中构建一个“.dll”,有时是一个“.exe”,用于带有 VS2015 的 Windows。出现错误时,我正在将项目构建为 exe。然后我只是将我的项目切换为构建为 DLL,并且以这种方式构建我得到了更有意义的错误!要在 VS2015 中构建 DLL:

项目->属性(ALT+F7)->​​配置属性(左侧列)->常规->常规->目标扩展:.dll“->常规->项目默认值->配置类型:动态库(.dll) "-> 配置属性(左侧列)-> 链接器-> 系统-> 子系统:Windows (/SUBSYSTEM:WINDOWS)

您可能需要将 C++ 对象设置为 "DLL exportable" ;这样做:

 //define it when you build as DLL
// undefine it when you build as an exe and undo the steps above in the Project Properties
#define DLL

#ifdef DLL
#define DLL_EXPORT __declspec(dllexport)    //shortens __declspec(dllexport) to DLL_EXPORT
#else
#define DLL_EXPORT 
#endif 


#ifdef __cplusplus  //if C++ is used convert it to C to prevent C++'s name mangling of method names
extern "C"
{
#endif

class DLL_EXPORT MaGamesClass
{
public:
  /* your class, as usual */
    MaGamesClass();
    ~MaGamesClass();

    float _soon = 2019;
};

#ifdef __cplusplus
}
#endif

就我而言,我的 DLL 导出类之一在其构造函数中有一些参数;我发现 DLL 导出的类不喜欢那样......

因此,我只是添加了一些函数作为构造函数,以满足我的所有需求:MaGames.h:

public : 
        MaGamesClass();
    //Because parameters cannot be set to constructor in DLL (->build error)
    static MaGamesClass MaGamesClass_instantiate(std::string where);
    static MaGamesClass* MaGamesClass_new(std::string where);
        /*
             Return this
        */
    MaGamesClass* init(std::string where);
    ~MaGamesClass();

/*
Some guys prefer initialisation of members in the constructor (MaGamesClass MaGamesClass(): _soon(2019){ ... }
I prefered here to avoid as much as possible constructor's build errors
And i find it's more readable anyway :p
*/
      float _soon = 2019; //
      std::string _where;

MaGames.cpp:

using namespace std;

MaGames MaGames::MaGames_instantiate(string where)
{
    MaGames vrSport= MaGames();
    return *vrSport.init(where);
}

MaGames MaGames::MaGames_new(string where){
    MaGames* vrSport= new MaGames();
    return vrSport->init(where);
}

MaGames * MaGames ::init(string where)
{
    _where = where;
        return this;
}

用法示例:

MaGames myCenter = MaGamesClass::MaGamesClass_instantiate("France");
MaGames* myCenter = MaGamesClass::MaGamesClass_new("Closer?");

瞧瞧瞧,希望这对大家有帮助

梦想,去,并且玩得开心


推荐阅读