使用OpenCV3.4.5在Qt5中与Cuda相关的编译错误

时间:2019-01-10 14:31:47

标签: qt opencv

我使用VS2015和Cuda8.0构建了启用了Cuda支持的OpenCV3.4.5(请参阅帖子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功能而不是使用预构建版本,该如何解决?

4 个答案:

答案 0 :(得分:1)

我也遇到过类似的问题,我可以找到一种解决方法。 以我为例,我在同一个项目中为Windows与VS2015有时构建一个“ .dll”,有时构建一个“ .exe”。错误出现时,我正在将项目构建为exe。然后,我只是将项目转换为DLL,并且以此方式构建时,我遇到了更多有意义的错误! 在VS2015中构建DLL:

  

Project-> Properties(ALT + F7)->​​ Configuration Properties(列在   左)->常规->常规->目标扩展名:.dll“->常规->   ProjectDefaults->配置类型:动态库(.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?");

Voila voila,希望对大家有帮助

Dream,Go和AndHaveFun

答案 1 :(得分:1)

对于遇到此问题的任何人,我的解决方案是包含 cudaarithm.hpp BEFORE opencv.hpp任何其他包含的标题,其中包含 opencv.hpp。这在@jingweimo 接受的答案中并不完全清楚

答案 2 :(得分:0)

too many actual parameters for macro 'max'

在名为min的函数与您所包含的某些文件中定义的宏之间可能存在冲突定义,该宏可能定义在Windows api标头中。

您可以尝试在项目的预处理器部分使用 NOMINMAX 编译项目。

答案 3 :(得分:0)

#include <opencv2/cudaarithm.hpp>

//or 
#include <opencv2/opencv.hpp>