简体   繁体   中英

Wrapping C++ library in XCode

I need some help with wrapping C++ libraries in XCode.

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.

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. 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 ;-)

However it will bring it's own pain (eg non-primitive types, other compilers) and maintenance issues as you update your C++ code.

You'll better served by:

  • writing an ObjectiveC wrapper and use MonoTouch's btouch tool to generated bindings;

  • writing a C wrapper and using .NET p/invoke to call your code;

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.

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. I have recently done this for a C++ library that implemented a Sybase database engine. 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.

Specifically, you can look at these questions:

Linking to a C++ native library in MonoTouch

Wrapping a C++ library in Objective-C is not hiding the C++ symbols

Application with static library runs on simulator but not on actual device

Undefined symbols when linking PhoneGap static library in MonoTouch

Linker options 'Link all assemblies" and "Link SDK assemblies only" causes undefined symbols in 3rd party static library

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. From my experience, the code worked more reliably than the dll, although the issues with the dll may have been resolved by now. But take a look at this question regarding the btouch issue:

Exception System.InvalidCastException when calling a method bound with btouch that returns an object. MonoTouch bug?

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.

Bruce, as you assumed, I have problems with wrapping C++ code. After hours and hours of reading and trying, I couldn't wrap the C++ code.

Anyway, I managed to create a simple Obj-C library made of some dummy class, and then import it into another library. 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.

I added linker flags: -ObjC -lNameOfLib Does C++ Standard Library Type in Build - Linking has to be Static? 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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