包括C ++ / CLI代码中非托管C ++代码的头文件 [英] Including headers from an unmanaged C++ code inside C++/CLI code

查看:123
本文介绍了包括C ++ / CLI代码中非托管C ++代码的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为非托管C ++库写一个CLR包装。

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

我从非托管lib中包含了两个文件:

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

//MyCLIWrapper.h
#include "C:\PATH\TO\UNMANAGED\Header.h"
#include "C:\PATH\TO\UNMANAGED\Body.cpp"

然后我为非托管库函数编写CLI实现:

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

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

但是,如果我的Unmanaged函数包含调用在其他非托管头文件中定义的其他函数。这会导致编译器链接错误。

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?

编辑:
PS
我的托管代码在一个单独的Visual Studio项目(输出 - DLL)中,编译设置设置为/ CLR。非托管代码在单独的Win32项目(输出 - DLL)中。

P.S. 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).

另外,经过更多的研究,我得出结论,理论上我可以将我的Win32非托管项目设置为CLR,我的托管类和头文件作为入口点,然后它将所有编译成单个DLL文件。这可能解决(?)链接错误。但是,我宁愿保留松耦合以及从将非托管项目设置为CLR可能引起的额外系列问题。

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.

编辑#2 :
我引用的非托管类(body.cpp,header.h)包含定义导致问题的函数的必需文件。但是,我的托管代码不会接受在非托管body.cpp和header.h中的include。

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.

推荐答案

链接器错误是一个不同的水壶从编译器错误。你忘了记录你看到的确切的链接器错误,但是当你使用/ clr编译代码时,一个非常普遍的错误是非C ++成员函数的默认调用约定改变了。默认值为__clrcall,这是为托管代码优化的约定。而编译没有/ clr的函数默认为__cdecl。这改变了函数名称被破坏的方式。你在链接器错误消息中看到这个消息,表明它正在查找一个__clrcall函数,并且找不到它。

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.

你需要显式声明你的函数在.h文件中用__cdecl。或者告诉编译器这些函数不是托管代码。这是最好的解决方法:

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)

这篇关于包括C ++ / CLI代码中非托管C ++代码的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆