Including headers from an unmanaged C++ code inside C++/CLI code

I'm writing a CLR wrapper for an unmanaged C++ library.

There are two files I'm including from the unmanaged lib:

//MyCLIWrapper.h
#include "C:PATHTOUNMANAGEDHeader.h"
#include "C:PATHTOUNMANAGEDBody.cpp"

Then I'm writing CLI implementations for the unmanaged library functions:

//MyCLIWrapper.h
// includes ...
void MyCLIWrapper::ManagedFunction()
{
  UnmanagedFunction(); // this function is called successfuly
}

However, if my Unmanaged function contains calls to other functions that are defined in other unmanaged header files. This causes a compiler linkage error.

If I add includes to the unmanaged headers that define these functions, my errors get resolved. However, there is a lot of functions, and a lot of includes required.

Is there a different way to approach this?

EDIT: PS My managed code is in a separate Visual Studio project (output - DLL), and the compile settings are set to /CLR. Unmanaged code is in a separate Win32 project (output - DLL).

Also, after more research I concluded that theoretically I could set my Win32 unmanaged project to CLR and just add my managed classes and headers in there as an entry point, and then it would all compile into a single DLL file. That would probably solve (?) the linkage errors. However, I would prefer to preserve the loose coupling as well as the additional series of problems that can raise from setting my unmanaged project to CLR.

EDIT #2: The unmanaged class that I'm referencing (body.cpp, header.h) contains includes to the required files that define the functions that are causing the problems. However, my managed code doesn't pick up on the includes that are in the unmanaged body.cpp and header.h.


Linker errors are a different kettle of fish from compiler errors. You forgot to document the exact linker errors you see, but a very common mishap when you compile code with /clr in effect is that the default calling convention for non-C++ member function changes. The default is __clrcall, a convention that's optimized for managed code. While functions compiled without /clr defaults to __cdecl. Which changes the way the function name is mangled. You see this back in the linker error message, is shows that it is looking for a __clrcall function and can't find it.

You'll need to either explicitly declare your functions in the .h file with __cdecl. Or tell the compiler that these functions are not managed code. Which is the best way to tackle it:

#pragma managed(push, off)
#include "unmanagedHeader.h"
#pragma managed(pop)

Solution was fairly simple:

  • I added both unmanaged and managed projects to a single solution in Visual Studio.
  • Set the unmanaged project's "Configuration Type" to "Static Library" (.lib).
  • Right click on the managed project -> References -> Add Reference -> Projects -> -> Add Reference.
  • Then in my managed class, I include the header.h (only) just like I did in my question.
  • Compiled successfully!
  • Thank you

    链接地址: http://www.djcxy.com/p/59614.html

    上一篇: 从非托管调用托管库时C ++ / CLI包装memleak

    下一篇: 在C ++ / CLI代码中包含来自非托管C ++代码的头文件