1

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.

1
  • @Matthew Flaschen: When I say it's trivial I just mean that it's something that I'm doing for fun, not for a job or anything like that. Commented May 10, 2009 at 3:24

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

Comments

0

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.

Comments

0

How about using wxWidgets instead?

Comments

0

You can just compile your GTK/C code as C++ without using GTKmm, and use the C++ code natively.

Most sane C libraries can be used from native C++ code, and GTK+ is fundamentaly a C library.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.