Compiling VS2017 CLR Project with OpenCV 3.4.1

asked 2018-06-01 17:18:28 -0500

kabeero gravatar image


I created a Console project in VS2017 utilizing OpenCV 3.4.1 and it worked as intended (include, bin, lib directories and no CLR).

However, now I'm trying to call these functions from another application and I need to compile the .dll with metadata to provide interoperability, thus hinting I need to recompile with /CLR.

The OpenCV cvdef.h file sets CV_CXX_STD_ARRAY=1 if you're in VS2017 or C++11 compiler, which adds #include <array>. This seems to be loading a different variant of array from mscorlib (added to References when you enable CLR) instead of stdlib, which is now breaking compilation.

# if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS2015*)
#  define CV_CXX_STD_ARRAY 1
#  include <array>
# endif

The errors I'm receiving are for mat.inl.hpp line 1947:

template<typename _Tp> template<std::size_t _Nm> inline
Mat_<_Tp>::operator std::array<_Tp, _Nm>() const [<-- line 1947]
   std::array<_Tp, _Nm> a;
   return a;

C3149: 'cli::array<_Tp,_Nm>': cannot use this type here without a top-level '^'
C2244: 'cv::Mat_<_Tp>::operator cli::array<_Tp,_Nm> ^': unable to match function definition to an existing declaration

I think C2244 is triggered when the compiler makes the suggested edit for C3149, thus saying it doesn't have a definition for array<_Tp,_Nm>^

Any advice or suggested edits?


I don't like this solution, but I can get the CLR .dll to compile by commenting out the cvdef.h lines:

# if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900/*MSVS2015*)
#  //define CV_CXX_STD_ARRAY 1
#  //include <array>
# endif
edit retag flag offensive close merge delete



opencv is all RAII based, and that's problematic, if you want to mix it with CLI/CLR. NOT a match made in heaven. it will also get worse with the next 4.0 version which will try to support c++11 fully.

best advice is: DON'T use CLR with opencv. if you abolutely have to, design a clear watershed (your own interfaces) between opencv and managed code, so each does not seee the other directly.

berak gravatar imageberak ( 2018-06-02 01:25:29 -0500 )edit

thank you for your reply. it is a relief to know I wasn't doing something entirely wrong and it was intended behavior.

it is unfortunate that in OpenCV 2.4 this wasn't an issue, I see many projects using CLR.

kabeero gravatar imagekabeero ( 2018-06-04 16:17:02 -0500 )edit