Best approach to port large C/GTK+ app to C++?

Howdy, y’all

I’m a long-time sometimes-maintainer of the Geeqie image-viewer. For a variety of reasons, it makes sense to investigate porting the codebase from C to C++. The codebase is around 20 years old, and is around 100k lines of C. So if this project is going to go anywhere, I’d need to find incremental ways to make progress. I can’t just re-type everything from scratch. It doesn’t matter too much whether the end state is GTK+, GTKMM, or some other GTK wrapper. But it needs to be C++.

I did the dumb thing first (just compile the C files as C++), and it took surprisingly little work to get that working. :white_check_mark:

But now, as I’m trying to turn a C-style class (aka a struct and a 4,000-line implementation) into a C++-style class, I’m running into trouble. Specifically, I need to use methods for callbacks instead of just unbound functions. To keep things concrete, I’m working on a g_hash_table_foreach call right now.

The obvious approach here is to pass some kind of functor object through user_data. But for cases that already include a user_data pointer, that means I’d have to re-do those to wrap the original user_data pointer, along with the this and the method pointer. That’s doable, but it could turn into a lot of code changes (and also a lot of memory allocations that I’d need to manage.)

Is there a better way? Whether for this question specifically, or for this project more generally?

Well, for C++ I recommend GTKmm. Using libsigc++ you can directly connect class methods to signals, no need to have static member functions (or friend helpers outside of the class). Even for GLib functions you have wrappers in GLIBmm. You should also consider whether using GLib data structures still makes sense, or if instead you’d better switch to STL containers. You can switch to GTKmm in an incremental fashion because it can interoperate with plain GTK.

You may take a look at the source code of some GTKmm applications (Inkscape, Ardour, GParted, gnome-system-monitor, etc.), but most importantly the great Programming with gtkmm

A general recommendation: I don’t know if the codebase you work on is well layered (with an internal library, for example). C++ bindings (like gtkmm) could be created for your internal library, and the layer above can be moved to C++ more easily. Then you can port the internal library. By doing it this way, it can be done either coarse-grained or fined-grained, depending on how your codebase is structured.

See also this article that I wrote, to better see what I mean:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.