views:

402

answers:

1

I'm using the OpenCV library and one of its header files, cxoperations.hpp, generates "warning C4793: '`anonymous namespace'::CV_XADD' : function compiled as native", if my C++ project is compiled with CLR support. I can prevent the warning by surrounding the OpenCV header include like this:

#pragma managed(push,off)
#include <cv.h>
#pragma managed(pop)

But the project that actually uses OpenCV isn't compiled with CLR support, it's a native C++ static library. The project that does have CLR support, and generates this warning without the pragma statements, simply uses this static library. So I'm a bit surprised that the warning was created at all, especially given the fact that the entire static library is not compiled with CLR support, and yet it's only this one header that causes the problem.

Thus this solution seems sub-optimal to me. Is this how you would handle this warning, or can you recommend a better practice?

+1  A: 

I think what you want is this:

#pragma unmanaged
#include <cv.h>
#pragma managed
// managed code wrapping unmanaged opencv functions

A C++/CLI project can contain both managed and unmanaged parts, and the compiler takes care of marshalling data between the 2 for you. The managed entry points will be callable from normal .NET apps (like C# and the rest) and will use garbage collection, and they'll call unmanaged functions to do the heavy lifting.

Blindy
Is there a difference between the pragma statements that I posted, and those you suggested? I couldn't find anything online to say clearly one way or the other, though they would appear to me to have the same effect.
Michael Repucci
Not sure, but this is how I've seen it done in MSDN.
Blindy
After a bit of usage, I've found this construction is slightly sub-optimal. That is, if you use this construction in code compiled without CLR support, it returns the warning C4949, whereas the push-pop construction gives no warning.
Michael Repucci
Well this is intended for C++/CLI, why would you compile managed code with an unmanaged compiler?
Blindy
The problem is that the OpenCV library uses assembly, which isn't allowed in managed code. But managed code should be allowed to use the OpenCV library with the assembly functions compiled as native code. Likewise, native code should also be able to use the OpenCV library, and not expect the warning. So I still vote for the push-pop method.
Michael Repucci