Calling a C++ shared Library in Ruby - c++

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.

Related

How to wrap a huge static C++ library in Objective-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

How to embed a C++ library in a C library?

I have a question related to embedding one library in another.
I have a code that is pure C and my users rely on that, they don't want to depend on C++ libraries. However, the need arose to embed a 3rd party library (ICU) into mine. None of the ICU functions would be exported, they would only be used internally in my library. Unfortunately ICU is a C++ library, though it does have a C wrapper. ICU does not use exceptions, but it does use RTTI (abstract base classes).
The question is how could I create my static library so that
ICU is embedded in my library (all references to ICU functions are resolved within my library)
all references to libstdc++ are also resolved and the necessary code is embedded into my library
if a user does not even have libstdc++ installed on their system things work just fine
if a user does happen to use my library within a C++ project then there are no conflicts with whatever libstdc++ (presumably the system libstdc++) he uses.
Is this possible at all? The targeted platforms are pretty much everything: windows (there my library is dynamic), and all sort of unix versions (linux, solaris, aix, hpux - here my library needs to be static).
gcc-4.5 and later does have --static-libstdc++, but as far as I understand it is only for creating shared libs or executables, and not static libs.
Thanks for any help!
The solution to this problem is pretty simple, but may not fall inside the parameters you have set.
The simple rules are:
You can dynamically link a C++ library to a C caller, but you have to wrap the library inside an extern C layer. You design the extern C API and implement it using C++ internals, which are forever hidden from view. [You can also use COM or .NET to do this on Windows.]
You cannot statically link a C++ library to a C caller. The libraries are different and the calling sequences/linker symbols are different. [You often can't even statically link between different versions of the same compiler, and almost never between different compilers.]
In other words, the solution is simple: use dynamic linking. If that's not the right answer, then I don't think there is one.
Just to make things interesting, you can even implement your own plug-in architecture. That's just another name for dynamic linking, but you get to choose the API.
Just to be clear, the only viable portable option I can see is that you link ICU inside its own dynamic library (DLL or SO). Its symbols, C++ libs, RTTI and exceptions all stay inside that. Your static lib links to the ICU dynamic lib by extern C. That's exactly how much of Windows is built: C++ inside DLL, extern C.
You can debug across the boundary, but you cannot export type information. If you need to do that, you will have to use a different API, such as .NET or COM.
I don't know if this will work, but let me at least suggest that you try it!
The excellent LLVM project (origin of clang compiler) has many front-ends and back-ends for different languages, such as C++ and C. And according to this S.O. question it should be possible for LLVM to compile C++ into C which in turn can be compiled as normal.
I imagine this route is a bumpy one, but if it works, it might solve your problem without the need to link dynamically. It all depends on if ICU will compile with LLVM C++.
If you do decide to give it a go, please let us know how you fare!

Using C++ bindings with GObject Introspection

I decided I want to use the Goffice library in my project. But I write it in C++, so I prefer to have a C++ class interface, just like I use gtkmm and not GTK+ directly.
The documentation (see link above) says I can use GObject Introspection. So I started reading anout it. I read and read and read, and I just couldn't understand how to use any binding of GOffice. I looked for a goffice gi-repository/typelib file on my system, and in the list of files installed by PackageKit. Found nothing. I checked in PackageKit if goffice or goffice-devel packages depend on the gobject introspection package. Maybe they depend indirectly, but they don't depend on it directly (otherwise I'd see it on the list).
I tried and tried, but I couldn't find a resource which could simply explain how to take a library written in GObject, such as GOffice, and use it on another language, e.g. Python, or in my case C++. Of course, I can use the C functions directly, but the point is that I want to have an interface similar to gtkmm.
(I use GNU/Linux, writing a desktop application with gtkmm and GNU build system, goffice version 0.10)
There is currently no GObject Introspection tool for C++. You can see a list of users at https://live.gnome.org/GObjectIntrospection/Users.
Based on one of GOffice's automake files, the GIR name GOffice is GOffice-0.10, so you should expect $(pkg-config --variable=girdir gobject-introspection-1.0)/GOffice-0.10.gir and $(pkg-config --variable=typelibdir gobject-introspection-1.0)/GOffice-0.10.typelib, but it's possible your distribution's packages don't include those files, in which case you might want to consider filing a bug.
As for documentation on how to use GObject Introspection for Python, you should check out the PyGObject site. They link to the The Python GTK+ 3 Tutorial, which should help you get a feel for how to use PyGObject. As for other languages, the documentation will vary depending on the language and implementation.
gtkmm, glibmm, and other -mm libraries currently still use the gmmproc tool to generate bindings for GObject-based libraries. This tool is older than GObject Introspection (GI is considered stable from GTK+ 3) and requires manual work on writing headers with special macros which will be used by the tool to generate C++ source code. For more details and a how-to see Wrapping C Libraries with gmmproc.
As for automatic C++ binding generation using GObject Introspection, I've found only one WIP: gi-mm.
Alternatively there are GObject Consume and Smoke-GObject which both can be used to integrate GObjects with C++ through Qt framework.
cppgir is GObject-Introspection C++ binding wrapper generator (also it is listed here).
It is lightweight (straight binding), optionally it can be used inline (header-only), therefore the program can directly link to any GObject-based library (GTK, GStreamer, etc).
You can read more information from README and documentation.

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

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.