简体   繁体   English

在XCode中包装C ++库

[英]Wrapping C++ library in XCode

I need some help with wrapping C++ libraries in XCode. 我需要一些在XCode中包装C ++库的帮助。

What I want to achieve is to create new library in XCode, import C++ library (I have .a and .h files), wrap it to Obj-C so I can import that library to MonoTouch. 我要实现的是在XCode中创建新的库,导入C ++库(我有.a和.h文件),将其包装到Obj-C,以便可以将该库导入MonoTouch。

The reason why I do it round way is that when I try to import C++ lib into MonoTouch, because of name mangling I keep getting WrongEntryPoint exceptions. 之所以这样做,是因为当我尝试将C ++ lib导入MonoTouch时,由于名称处理问题,我不断收到WrongEntryPoint异常。 Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler. 如果我错了,请纠正我,但是我找不到找出乱码的名称,这取决于编译器。

Thank you in advance 先感谢您

Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler. 如果我错了,请纠正我,但是我找不到找出乱码的名称,这取决于编译器。

Technically you could. 从技术上讲,您可以。 Many compilers share the same mangling syntax , maybe the most useful and long-lasting gift from Itanium ;-) 许多编译器共享相同的处理语法 ,也许是Itanium提供的最有用和最持久的礼物;-)

However it will bring it's own pain (eg non-primitive types, other compilers) and maintenance issues as you update your C++ code. 但是,当您更新C ++代码时,将带来自己的痛苦(例如,非原始类型,其他编译器)和维护问题。

You'll better served by: 您最好通过以下方式服务:

  • writing an ObjectiveC wrapper and use MonoTouch's btouch tool to generated bindings; 编写一个ObjectiveC包装器,并使用MonoTouch的btouch工具生成绑定;

  • writing a C wrapper and using .NET p/invoke to call your code; 编写C包装程序并使用.NET p / invoke调用您的代码;

The choice it yours but if you think about reusing the C++/C# code elsewhere (eg Mono for Android) then using C and p/invoke will be reusable. 这是您的选择,但是如果您考虑在其他地方重用C ++ / C#代码(例如Android的Mono),则可以重用C和p / invoke。

I would definitely recommend going the route of wrapping the library in an Obj-C library and using btouch to import the library into MonoTouch. 我绝对建议您将库包装在Obj-C库中,然后使用btouch将库导入MonoTouch。 I have recently done this for a C++ library that implemented a Sybase database engine. 我最近对实现Sybase数据库引擎的C ++库进行了此操作。 If you look at my questions you will find quite a few pertaining to wrapping C++ libraries as I posted a few times regarding issues I encountered. 如果您看一下我的问题,在我就遇到的问题发表过几次之后,您会发现很多与包装C ++库有关的问题。

Specifically, you can look at these questions: 具体来说,您可以查看以下问题:

Linking to a C++ native library in MonoTouch 链接到MonoTouch中的C ++本机库

Wrapping a C++ library in Objective-C is not hiding the C++ symbols 在Objective-C中包装C ++库不会隐藏C ++符号

Application with static library runs on simulator but not on actual device 带有静态库的应用程序在模拟器上运行,但不在实际设备上运行

Undefined symbols when linking PhoneGap static library in MonoTouch 在MonoTouch中链接PhoneGap静态库时的未定义符号

Linker options 'Link all assemblies" and "Link SDK assemblies only" causes undefined symbols in 3rd party static library 链接器选项“仅链接所有程序集”和“仅链接SDK程序集”会导致第三方静态库中的未定义符号

I would also recommend, if you are going to go the route of an Obj-C wrapper, that you get btouch to output code and include that in your project rather than including a dll from btouch. 我还将建议,如果您打算采用Obj-C包装程序的方法,则可以让btouch输出代码并将其包含在项目中,而不是包含来自btouch的dll。 From my experience, the code worked more reliably than the dll, although the issues with the dll may have been resolved by now. 根据我的经验,尽管dll的问题现在可能已经解决了,但是代码比dll更可靠地工作。 But take a look at this question regarding the btouch issue: 但是,请看一下有关btouch问题的问题:

Exception System.InvalidCastException when calling a method bound with btouch that returns an object. 调用与btouch绑定的方法返回对象时,异常System.InvalidCastException。 MonoTouch bug? MonoTouch错误?

If you have specific questions/problems in building the Obj-C wrapper then ask them here and post some code and I am sure that I or other members of the community would be able to help you with it. 如果您在构建Obj-C包装程序时遇到特定的问题/问题,请在此处询问并发布一些代码,我相信我或社区的其他成员将能够为您提供帮助。

Bruce, as you assumed, I have problems with wrapping C++ code. 如您所料,布鲁斯(Bruce)在包装C ++代码时遇到问题。 After hours and hours of reading and trying, I couldn't wrap the C++ code. 经过数小时的阅读和尝试,我无法包装C ++代码。

Anyway, I managed to create a simple Obj-C library made of some dummy class, and then import it into another library. 无论如何,我设法创建了一个由某些虚拟类组成的简单Obj-C库,然后将其导入到另一个库中。 That worked fine. 很好。 However, following same pattern, I included C++ .a file along with .h file (I'm not sure whether .h is mandatory because we can link header files in build options, right??) and when I compiled it, it went fine, the build succeeded, but XCode didn't produce new .a library. 但是,按照相同的模式,我将C ++ .a文件与.h文件一起包含(我不确定.h是否是强制性的,因为我们可以在构建选项中链接头文件,对吗?),当我对其进行编译时,很好,构建成功,但是XCode没有产生新的.a库。

I added linker flags: -ObjC -lNameOfLib Does C++ Standard Library Type in Build - Linking has to be Static? 我添加了链接器标志:-ObjC -lNameOfLib构建中的C ++标准库类型-链接必须是静态的吗? And Symbols Hidden By Default as well? 以及默认情况下隐藏的符号?

It would be great if we could write step-by-step tut, since there are tons of various instructions, but I haven't been able to push it through the end. 如果我们可以编写循序渐进的教程,那就太好了,因为有大量的各种指令,但是我一直无法将它推到最后。

I'm confused a bit.. 我有点困惑。

Thank you guys... 感谢大伙们...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM