views:

206

answers:

4

I have a simple (and also trivial) banking application that I wrote in C++. I'm on ubuntu so I'm using GNOME (GTK+). I was wondering if I could write all my GUI in C/GTK+ and then somehow link it to my C++ code. Is this even possible?

Note: I don't want to use Qt or GTKmm, so please don't offer those as answers.

+5  A: 

Yes, it's a very easy thing to do. All you have to do is expose some of the C++ functions as "extern C" so that the event handlers and callbacks in your UI code can call them.

In the case that you can't change the existing C++ source - no problem. just write a C++ shim for your UI, extern those functions, and call backend functions from there.

Ben Collins
@Ben Collins: Can you provide some examples on how to do this? Thanks.
Lucas McCoy
Sure. See here: http://pastebin.com/f364909f2. Also, a good and brief reference for this kind of thing is here: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
Ben Collins
+1  A: 

I don't see why not, with appropriate extern "C" usage so your C code can call into C++. Now, granted, you're probably making it a bit harder on yourself, but it's theoretically sound.

Matthew Flaschen
A: 

Like others propose you can write a C wrapper for your C++ library. But you can also write the front-end in C++, even if you only use the C subset. I can understand if you don't like the language mixing, but it is the easiest way, because you save the time to write the wrapper.

quinmars
A: 

How about using wxWidgets instead?

Milan Babuškov