I am working on a program which from a c++ code generates a .so and bindings for multiple languages (C, C++, Haskell, Java, Php, Python...) and I'm trying to add support for golang.
I think the best way to get that result is to either use the C++ or the C version and create go bindings, but this has to be done automatically. I've tried using Swig but I didn't manage to get it working (likely because we're including a .so and not a .cc) and I've only found old documentation (this uses go tool 6g).
I've also tried creating bindings with cgo which doesn't work with C++, and I couldn't get it working with the C bindings (again, most likely because of the .so step).
At the end, I must generate another .so with a few exported functions, and which must be standalone which makes compiling the C++ version and talking with it through a pipe (almost?) impossible.
The functions to export are often things like:
void init_game(void);
void play_turn(void);
void end_game(void);
You can find a c++ header here and the auto-generated interfaces to C: interface.hh and interface.cc and prologin.h.
An example source code of lib.so can be found here.
Related
I have a project written in C (on Linux) and now want to use a third-party C ++ library that provides .h and .c source files.
I renamed the .c file to .cpp and then include this library in the C project. However, the following error appears when compiled:
unknown type name ‘class’
Added: The third-party library is here https://github.com/0xmalloc/c-log
The author say it's friendly to both C and C++
There are two options here:
you make your whole project a C++ one - this doesn't mean you need to convert your C code to C++ but rather that you will probably have to touch this and that part of it and also go for a C++ and not C-only compiler
provide wrappers - usually if you write C++ code in C style (no classes etc.) you will only have to do that extern "C" void foo() routine for your C++ functions. However the moment you start working with features that C doesn't support you will have to provide wrappers for your C++ functionality so that it can be exposed to the C part of your application. Such procedure can be from very easy to incredibly complex. For example many modern C/C++ API also provide Python API. In order to do that without having to rewrite everything in Python the authors create wrappers that translate the C/C++ functionality to Python. Depending on how well this is done features of the target language (Python in case we go from C/C++ to Python) can be used to allow better error handling, type checking, readability, native (to the target language) data containers etc.
I do believe that the author of the library misled you since the library is clearly for C++ (the classes in the header are definitely the most obvious thing that just screams 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.
I have a bunch of python code that I would like to "compile" into a shared library with a C interface that can be linked with other C/c++ programs and work without depending on too many other libs (perhaps python and some other dlls but they should all be included into a directory with the final lib).
I don't really want to rewrite the python code into C++ for this. I can of course, but it would be best to have a standalone lib that can be used like a dll/so lib.
I have tried cython and wanted to compile python to C and then just compile C code into a dll but that doesn't seem to work quite yet (I haven't been able to make it work flawlessly yet). And then I also tried bbfreeze - but does bbfreeze support creating an .so file? Wasn't able to find out how to do it. Does anyone know?
Do you know of any other options that are more straightforward? the python code only needs to be compiled once. And best of all would be if it creates a single .so file no matter how big it is that just works without too many dependencies.
You can use Python as a library from inside your C++ application: it's called the Python/C API.
The idea is that you initialize your own interpreter, then you load a script in it. You can expose your C++ objects through global variables or modules inside the Python interpreter for your code to interact with, your you can just run Python functions directly from your C++ code.
Now, I understand that you might want to have your Python scripts embedded inside the shared library: this is not necessarily an easy task, with the traditional GNU toolchain. There are multiple techniques to achieve this, but none is official, and all seem way too complicated as opposed to just having the script in an external file.
If your goal is to hide the scripts from the end-user, you could sign them or encrypt them with a private key and embed the public key inside your library, but keep in mind that the key can easily be retrieved by anyone with enough motivation.
Can APIs written in C or C++ always be made into a library, given source code, and consequently be called from Python, due to the intrinsic callable nature of an API?
In other words, given a C/C++ API can you run a setup.py script using distutils on the source and effectively use the C/C++ API from python programs?
It's a bit more involved than running a setup.py script, but there are tools to wrap C/C++ libraries so you can use them from Python. Some popular ones are:
Cython - you write Python-style code, but you can cimport C libraries and call their functions. Cython code is then translated to C and compiled. There's an unsupported tool called cython-codegen to automate writing a simple wrapper.
boost.python - For wrapping C++ libraries.
SWIG
ctypes - Load compiled libraries and call them directly from Python. ctypesgen tries to automatically write a wrapper based on header files.
Certain projects use other tools to generate Python bindings, and they could probably be reused (with varying amounts of effort). PyQt4 uses SIP, PySide (another Qt binding) has shiboken, and GTK et al. have Gobject introspection.
It's not exactly that simple, but basically you can do that using SWIG, which will generate the necessary wrappers to bind your python and C/C++ code. http://www.swig.org/Doc1.3/Python.html
There is no intrinsic or automated mechanism for binding a C++ API to Python. It must be done manually. The best tool I've seen for this purpose is Boost Python.
Of course, given the clean mapping between C++ Object Oriented programming and Python OO code, it shouldn't be too difficult to create a script which parses C++ headers and produces the binding declarations. GCC-XML will give you an xml representation of parsed headers, which can be easily queried by a python script to produce the bindings. That said, there's probably a great deal of detail I've not anticipated which could prevent this approach from working.
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.