Using C++ bindings with GObject Introspection - c++

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.

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.

zlib for WinRT?

I require zlib library for the development of Windows Store app.
Has anyone converted Win32 zlib project to WinRT yet?
Can anyone please describe the steps to convert the existing win32 static lib project to winRT?
Visual C++ is already a supported language for WinRT development, if you wan't to use zlib, just compile it together with your solution. There is nothing that is preventing you from reusing standard ISO C and C++ libraries from within the WinRT, if you are using the C++ language, you might have to expose certain aspects of your library as WinRT Components but only if you need to interface with facilities like XAML or other WinRT languages but that should be a walk in the park. Not something which is tremendously difficult to do.
The whole point of supporting C++ in the WinRT is to allow an existing ecosystem of largely native applications to be ported to the Windows Store. zlib is not an exception. Non-standard ISO C and C++ such as sockets are not supported but there you have alternatives that you can plug-in to, just check that the library you're using has some kind of portability support.
WinRT is very limited with regards to C library functions which are present. What this means is that virtually all cross-platform C libraries are (AFAIK, I'm not a WinRT dev) unusable for that target.
For the case of zlib, there is an alternative: see this question
EDIT: to clarify what I'm saying above, I dug up a list of all CRT functions that are absent for WinRT, which you can find here. As long as zlib or any other C library does not depend on these function calls, you should be able to use the WinRT tools to build that C library. I even found a project file for zlib on winrt by the Ogre team here, not sure how useful it is to you.
You could take a look into this WinRT (Un)Zip component. Its used in production code already.
See the unit tests inside on how to use the component. It compiles on all WinRT architectures including ARM. It has no custom asm for ARM though.

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.

Using Boost on ubuntu

I've heard a lot of good comments about Boost in the past and thought I would give it a try. So I downloaded all the required packages from the package manager in Ubuntu 9.04. Now I'm having trouble finding out how to actually use the darn libraries.
Does anyone know of a good tutorial on Boost that goes all the way from Hello World to Advanced Topics, and also covers how to compile programs using g++ on ubuntu?
Agreed; the boost website has good tutorials for the most part, broken down by sub-library.
As for compiling, a good 80% of the library implementation is defined in the header files, making compiling trivial. for example, if you wanted to use shared_ptr's, you'd just add
#include <boost/shared_ptr.hpp>
and compile as you normally would. No need to add library paths to your g++ command, or specify -llibboost. As long as the boost directory is in your include path, you're all set.
From the boost documentation:
The only libraries that need to be compiled and linked are the following:The only Boost libraries that must be built separately are:
Boost.Filesystem
Boost.IOStreams
Boost.ProgramOptions
Boost.Python (see the Boost.Python build documentation before building and installing it)
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.Thread
Boost.Wave
A few libraries have optional separately-compiled binaries:
Boost.DateTime has a binary component that is only needed if you're using its to_string/from_string or serialization features, or if you're targeting Visual C++ 6.x or Borland.
Boost.Graph also has a binary component that is only needed if you intend to parse GraphViz files.
Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.
So, if you're using one of the listed libraries, use the Getting Started guide to, well, get you started on compiling and linking to Boost.
The Boost website has some good tutorials, they are just kind of hidden.
The library documentation is a mixed bag. Some is good, but some is more of a reference than a guide. The best guide to (some of) the Boost libraries is the book Beyond the C++ Standard Library. At the very least, the introduction gives one paragraph descriptions of many of the libraries. From there, you can decide which library is most important for your current needs, and, if it's in the book, read the chapter on it, or read the documentation on the website.
If you read German, there's a good online guide. Google translate does a good enough job that a non-speaker like me can understand it.
Also, unless you have lots of experience with C++, I'd start with the simpler libraries (e.g. smart_ptr, tuple, conversion, tokenizer, regex, date_time, test), before trying the more complicated ones (bind, variant, any), or the really advanced ones (concepts, MPL, Fusion).
Using Easypeasy 1.1 (for netbooks) which is based upon Ubuntu I was able to use Synaptic Package Manager to install, I believe, libboost-dev. Then simply by adding:
#include "boost/foreach.hpp"
I was able to replace the existing lines in an existing application (which has an Ask class which has nothing to do with boost):
for (std::vector<Ask*>::iterator ii=ui.begin(); ii!=ui.end(); ++ii)
std::cout << (*ii)->prompt() << (*ii)->answer() << std::endl;
with:
BOOST_FOREACH (Ask* ii, ui)
std::cout << ii->prompt() << ii->answer() << std::endl;
As I understand it this is a header only feature. I have not used anything requiring link time changes yet.
I was just looking at that german boost guide, and found there was an english one as well (same book). It looks pretty good, have just read the introductory pages which are quite useful
The best tutorial I've read so far are those two books:
Introduction to the Boost C++ Libraries; Volume I - Foundations
Introduction to the Boost C++ Libraries; Volume II - Advanced Libraries
The libraries come with documentation and many of them have tutorials as part of the documentation. Just start reading.
Boost is not a programming language nor an application framework - because it's just a collection of libraries, there is no such thing as a Boost 'Hello World' program. Most libraries in Boost can be used more or less independently, and they vary in size from one function to massive libraries that could stand alone.
The best way to get to know Boost is simply to try and work it in as you write new code. Use smart_ptr whenever you can; use the MPL next time you want to do compile-time work. There's a lot of variety in Boost, but you should probably start looking at the Utility section; those are the lightest-weight and most commonly-used libraries.

Good patterns for a C/C++ plugin-based system?

When developing a C/C++ (=2?) plugin based framework with shared objects/dynamic libraries that need to support live swapping what examples would be helpful to look at for implementation details?
Thanks.
Note: live swapping is the key point here, no need to restart the system is a requirement
If you are on POSIX, dlopen(), dlsym() and dlclose() are all you need.
See man dlsym for details and examples.
There is a good article about loading dynamic libraries, and plugin infrastructure is an example.
EDIT OP added Windows as requirement so this approach won't help since Windows isn't POSIX-compliant. However there are similar functions in WinAPI - see here.
You might want to try Boost.Extension but beware : despite its name, it is not one of boost libraries.
Here is a link to its documentation.
For C++ plugins you can check this article which detail how to achieve it with the previously mentionned posix calls.
Quoting the article :
Given that we can use these functions to access functions in a C library, how do we use them to access classes in a C++ library? There are several problems to overcome. One is that we must be able to locate the symbols we need in the library. This is trickier than it might seem because of the difference between the way symbols are stored in C and C++ files.
If you want cross-platform library loading without having to develop for each platform's API individually, libltdl may help.
Libtool provides a small library, called libltdl, that aims at hiding the various difficulties of dlopening libraries from programmers. It consists of a few headers and small C source files that can be distributed with applications that need dlopening functionality. On some platforms, whose dynamic linkers are too limited for a simple implementation of libltdl services, it requires GNU DLD, or it will only emulate dynamic linking with libtool's dlpreopening mechanism.
libltdl supports currently the following dynamic linking mechanisms:
dlopen (Solaris, Linux and various BSD flavors)
shl_load (HP-UX)
LoadLibrary (Win16 and Win32)
load_add_on (BeOS)
NSAddImage or NSLinkModule (Darwin and Mac OS X)
GNU DLD (emulates dynamic linking for static libraries)
libtool's dlpreopen (see see Dlpreopening)
Boost.Extension seems to only support Windows PE dlls, UNIX ELF shared objects, and Mac OS X Mach-O bundles. Well, that may be sufficient for you...
Boost.Extension seems nice (never used it but will try soon). Another alternative would be the POCO SharedLibrary class.