boost.build Vs boost.python - c++

Context -- Trying to use Boost.Python set of C++ libraries to interface with C++ code.
Main idea is to test C++ code (.so files) by using them like python from a QA point of view.
Questions now;
BOOST_PYTHON_MODULE wrapper, do we really need to include in every .cpp to be interfaced from Python? Say we have test.cpp, can't we have Boost wrapper written test_qa.cpp so that actual dev code is not changed in the process?
Looked Boost.org site to get more clarity, what is the difference between Boost.Build and Boost.Python?

From the Boost Build documentation:
Boost.Build is an easy way to build C++ projects, everywhere.
From the Boost Python documentation:
... a C++ library which enables seamless interoperability between C++ and the Python programming language
I would say the difference between these two parts of Boost should be pretty obvious.

Related

What is the correct way to link 3rd party C++ libraries in Gradle?

I am currently trying to evaluate the use of Gradle C++ for a project that will have both Java and C++ components (with JNI to interface). I could just use CMake for the C++ portion, but then I would have 2 build systems which is less cleanly organized. As such, I prefer to use Gradle's C++ system in a multi-project build if it has the support that I need. The main thing that I can't find any detailed information (with code examples, etc.) is the linking of libraries. For Cmake, it is simple: use find_package or the pkg-config module. Every library (that I have tried to use) offers at least one of those systems. With Gradle, however, it only seems to document it for linking to C++ libraries that are built in the same project. What if, for example, I want to link to Vulkan, SFML, OpenGl, yaml-cpp, Boost, or any number of established and FOSS C++ libraries? The documentation also doesn't specify how to control dynamic or static linking.

Compiling python into a shared library

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.

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.

C++ API as an Python Extension Module

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.

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