Hi all!
I have been hacking some Qt before, but now I want to try to code in Gtk and std C++.
I have been searching the web but I can not realy find what I am looking for.
As I can see Gtk is written in C.
Is it good practice to mix Gtk c with c++ class structures,
or am I supposed to use the c++ interface for Gtk?
I know that I have to write a makefile to make it easier to compile.
Second of all, if I just use a simple texteditor to write my code I want an easy
access to API both for std C++ and for Gtk.
Where is a good place to find good API:s for both?

Many thanks in advance!!


Recommended Answers

All 3 Replies

Mixing C and C++ is not really recommended. Typically when using C libraries in a C++ program, it is best to create or use some kind of wrapper API. Whether this is a third party wrapper or one you create yourself is entirely up to you. I'd recommend at least trying the official one for now.

gtkmm, is the official C++ wrapper for gtk:

If you're a Windows user, you can download an installer from the gtkmm website above. If you're on Linux, your distro should have relevant packages in it's repositories, so check there first. Otherwise download some packages from the gtkmm site.

Regarding the 2nd part of your question:

Second of all, if I just use a simple texteditor to write my code I want an easy
access to API both for std C++ and for Gtk.
Where is a good place to find good API:s for both?

I'm not sure what you mean!

If you mean you want syntax highlighting, most modern text editors have built-in syntax highlighting for C++ nowadays, with the exception of things like MS Notepad. But most text editors don't offer anything beyond mere syntax highlighting.

If you want an editor with intellisense / code-hinting / code-completion functionality, then you might want to look into using an editor which is based on Scintilla. Like Scite, or perhaps something like the Code::Blocks IDE which uses Scintilla to provide code completion. (or any other IDE with code completion: VS, Eclipse, NetBeans etc.)

EDIT: I've just spotted the 'Linux' in the subject line! So, sorry about the Windows references in there.
Because I hadn't seen any platform specific references when I replied to this and because I saw you were using cross-platform libraries, I thought I'd keep my reply as generic as possible and cover Windows and Linux.
Doesn't really change anything, but just thought I'd mention it! DOH!

Most people will tell you that mixing C and C++ isn't good but there isn't any problem with doing so, They just recommend not to because most everyone likes conforming to the standards. Technically C++ isn't just C with Classes and some other stuff but under the hood it still pretty much is C, so in turn C code will function as expected under C++ but not vice versa.

I would recommend Code::Blocks off of personal experience but I find myself using Gedit more and more although Gedit doesn't have auto complete but then again its designed not to, It does have really nice syntax highlighting though.

Gtk is THE API, The std library generally comes with most C++ compilers and isn't really an API but more of set of base functions.

Assuming your on Ubuntu:
From Terminal:

sudo apt-get install gtkmm

Also unless your are doing a decently sized project makefiles shouldn't be necessary.

To compile the normal way on linux assuming that your using gcc/g++(G++ is for C++):

g++ main.cpp class1.cpp -o myapp -Wall #for multiple files
g++ main.cpp -o myapp -Wall # single files the -Wall switch warns you of potential errors and is good practice to use but not necessary

Hope I helped :P

Member Avatar

If you are looking for API reference you can find it at their respective sites.


Their website also has very nice tutorial(for GTK+2 I'm very sure).

Also, there is a package called "devtools" from GNOME, which installs docs and API browser. Its really good. Has some tutorials as well. I think in Ubuntu it installs automatically with Anjuta. BTW Anjuta should provide pretty nice configuration for GTK development. Try that out as well.

For stdc++ reference you can see http://www.cplusplus.com/reference/ as well as MSDN.

Compiling through command line is very easy. You just have to pass pkg-config. Something like

g++ main.cpp class1.cpp -o myapp -Wall `pkg-config --cflags gtkmm-2.0`

Just check the docs. I'm sure you'll find it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.