How to wrap a huge static C++ library in Objective-C? - c++

I need to use a huge static C++ library in Xamarin.iOS project, I have only one .a and tons of *.h files. So I have to wrap headers with Objective-C to run Xamarin Sharpie tool. Any suggestions how to do it?

To acess the methods from native libraries, you use Mono's P/Invoke functionality which is the same technology that you would use in .NET, which is roughly:
Determine which C function you want to invoke
Determine its signature
Determine which library it lives in
Write the appropriate P/Invoke declaration
You can find the more information on this blog here: http://kerry.lothrop.de/c-libraries/
CppSharp is another option that is mentioned in the comments

Related

Calling a C++ shared Library in Ruby

I have a shared library "mylib.so" which was written in C++ and I would like to make an application in Ruby that calls the library functions.
I researched and found FFI (https://github.com/ffi/ffi) and it only works with a C library. With Rice (https://rubygems.org/gems/rice/versions/2.1.0) only I found tutorial that I need to change the source code of the library, but I do not have access to this code.
Is there any way to implement a C++ library in my Ruby code to use its functions?
When you say you don't have access to the code, you mean that you do not have access to the headers of the library? If you do not have access to the headers defining classes, prototyping functions and exposing some API, then you wouldn't be able to actually bind this library to any other segment of code, whether the latter is written in c++, python, ruby or whatever.
If you do have access to the headers of the library, then you can easily use rice-ruby to build a ruby wrapper - by following the instructions here. You need only the headers and an up-to-date version of your library in order to do a proper wrap. Feel free to better define your problem and I'll try to help.

Call C++ class member functions in a static library from MATLAB

I have a C++ header file and the corresponding static library (.lib file). No source file.
The code comprises several classes, each with functions of its own.
I would like to be able to call these functions from MATLAB.
Would anyone be kind enough to point me in the right direction? Examples would be appreciated.
Thanks.
EDIT: Would it be a good idea to write a DLL to act as an interface between the static library and MATLAB? Then I can access the functions in my static library from the DLL and use calllib to access the DLL from MATLAB.
If so, how would I call the class member functions in the static library from a DLL?
Update
So I've decided to write a DLL to which I will link the static library and call the functions from MATLAB through that using calllib
The problem is, the static library is already compiled and I do not have the source. I try to link the .lib file (presumably built using Visual Studio) and build my DLL using MINGW32 but this doesn't work possibly due to (lack of?) interoperability of libraries created by different compilers.
This is the MinGW command I use to link: g++ -c -o -DBUILDING_INTERMEDIATE_DLL intermediate_dll.cpp -TaccClient.lib
intermediate_dll.cpp is the source file for my DLL and TaccClient.lib is the static library I'm using.
Does anyone have any suggestions as to how this could work, or will I have to wait and try it on visual studio later?
MATLAB offers MATLAB coder functionality that allows you to create MEX files from your C/C++ source and library files. There is also an option to called C shared libraries when building the MEX file for your application. I have used SIMULINK (with SIMULINK Coder) and Embedded Coder to do my library linking and it helps you to do it via GUI, so very neat job. However, you can still do it with MATLAB as long as you have a supported C/C++ compiler.
Using the C shared library option, you should be able to load, call, view the library and functions. For more details on what other functionlities are available, see this - http://uk.mathworks.com/help/matlab/using-c-shared-library-functions-in-matlab-.html
A similar question was answered here. May be you will find it useful?

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 (e.g. 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 (e.g. 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...

Objective c library with c interface

I have run into a problem... I'm trying to use QTKit in an application that we have at work. The only problem with that is the app is written in C++, not Obj-C. I have looked through Apple's documentation for answers, but I haven't found anything useful.
Basically what I'm looking to do is write a single controller class in Obj-C that has its methods exposed through a C interface to my app. I've written all that code already, but when I try and link it to even a sample C++ app, it finds the Obj-C symbols in the lib and complains about them being there. I thought about hiding the symbols using compiler flags, but I saw in Apple's docs that Obj-C isn't affected by that, since classes and messages are bound by the runtime and not the linker.
Has anyone successfully done this?
Thanks,
Robbie
You can use QTKit from within your C++ application by using Objective-C++:
Rename the files that access QTKit from .cpp to .mm. This does not change anything in your existing code but you can then use Objective-C from within these files.

How to load a c++ dll file into Matlab

I have a C++ dll file that uses a lot of other c++ librarys (IPP, Opencv +++) that I need to load into matlab. How can I do this?
I have tried loadlibrary and mex. The load library does not work.
The mex finds the linux things (platform independent library) and tries to include them. And that does not work.
Does anyone have any good ideas?
loadlibrary should work. I use it all the time to call functions from dlls written in C++ with C wrappers.
What errors are you getting when you try to use loadlibrary?
Make sure that the exported functions of the dll are C functions, not C++ functions. If not, then write C wrappers.
More info on exactly what you are doing when using loadlibrary would be helpful.
As mentioned by others, you should first wrap your C++ library as a C library - mathworks used to advise not to use C++ code directly in mex (dlopening C++ core directly is complicated), maybe it is still true.
Your description is quite unclear, too: what do you mean by "mex finds the linux thing", but that does not work. Saying that it does not work is not helpful: the exact commands and error message are.
You could go for the Java approach (since Matlab runs on a JRE and can access Java objects/methods -- just be aware that the Matlab JRE is not as up-to-date as the latest JRE, the one I'm running uses Java 1.5) and use JNA to access your DLL.
Or, if you wrote the top-level DLL, you could go for the COM/ActiveX approach.
I've had good success architecting the interface to my C++ functions as COM/ActiveX libraries -- you don't have to bother with that .h stuff.
See the External Interfaces guide on COM clients, particularly the part about managing/converting data.
It would be extra work to add the COM/ActiveX layer, but would make your library more portable within the Windows world and probably more easily used in MATLAB.
If you have a lot of function calls to your DLL, the COM/ActiveX approach might be faster (not sure), but otherwise I think the JNA approach would be easier.