Strict Standards: Non-static method WPU_Cache::getInstance() should not be called statically in /home/autronix/public_html/sites/ on line 71

Strict Standards: Only variables should be passed by reference in /home/autronix/public_html/sites/ on line 330
OS Dev Forums • View topic - [BLOG]: GTK+, C++, Windows 7 and Visual Studio 2008

OS Dev Forums Open Source Developer's Forums

[BLOG]: GTK+, C++, Windows 7 and Visual Studio 2008

Forum dedicated to people who found solutions to their problems and would like to share.

[BLOG]: GTK+, C++, Windows 7 and Visual Studio 2008

Postby jamil » 04 Mar 2010 02:10

This is a blog post. To read the original post, please click here »

So I had to make an interface with GTK+ for one of my school projects.

Since it turned out to be the continuation of a project that I had done previously on Windows, I had to keep using the tools I had. And so I was stuck using Visual Studio for this.

Now, let me give a little context:

I had a console application for which I needed to create a GUI in order to implement new features. Since I'm all for portability and Open Source stuff, I ended up choosing GTK+ as the framework for creating the interface.

It is also important noting, that, as perfectionist as I may be, I can also sometimes be a bit lazy; and so I downloaded glade in order to facilitate the creation of my interface, dialogs and what not.

Setting Up VS2008

My idea here is not to make a full guide on setting up VS for using GTK+, however I'll go over some of the basic steps.

  • Set directories (includes, sources, libs) in VS preferences - basically pointing to the GTK+ dirs
  • Create solution with main project
  • Open project properties and set linker Input libraries (all those .lib files required by the linker)
Creating the glade interface with callback, the program  and compiling - What's that error?

Next comes creating the interface with glade and setting the callbacks: for this section I based myself on this tutorial by Micah Carrick, which covers how to create the interface and setting up callbacks.

It also very well covers the basic GTK+ program to run the interface.

So now that that's done, we proceed to compile the code... And this is the step that seriously got on my nerves: The program compiled fine and as I ran the app, while the interface would be displayed, no callback would be functional.

I also noticed the following error message:
Gtk-WARNING **: Could not find signal handler 'on_window_destroy'

Needless to say I spent the next couple of hours trying to figure this one out without much success. I was getting bits and pieces on how to solve this on Unix by adding the "-export-dynamic" flag to the compiler (same thing for MinGW) but nothing covering why our friends at Microsoft didn't want to get this running.

Figuring out how to make it work

Finally, after hours of playing around and looking on the net what was what and how to get things done, I've managed to find how to get this going:

The answer was somewhat Here: __declspec(dllexport)

Taking back the code offered by Micah Carrick:

void on_window_destroy (GtkObject *object, gpointer user_data)

int main (int argc, char *argv[])
GtkBuilder *builder;
GtkWidget *window;

gtk_init (&argc, &argv);

builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "tutorial.xml", NULL);

window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
gtk_builder_connect_signals (builder, NULL);
g_object_unref (G_OBJECT (builder));

gtk_widget_show (window);
gtk_main ();

return 0;
Now this code was giving me problems with VS2008 for 2 reasons:

  1. C++ seems to append strings at the beginning of function names
  2. I had to find the equivalent for -export-dynamic on VS2008
Once that was done, I ended up with the following code:

#define DllExport __declspec(dllexport)

extern "C" DllExport
void on_window_destroy (GtkObject *object, gpointer user_data)

Now let me explain what is going on:

  • extern "C" takes care of the C++ function renaming and ensures the name of the function will be the one on the callback
  • DllExport and the __declspec(dllexport) take care of doing the "export dynamic" behavior from gcc

All that was left was compiling, running and Voilà!
OS Dev Forums Admin
OS Dev Forums Blog
User avatar
Site Admin
Posts: 13
Joined: 04 Mar 2010 11:13

Return to Lessons Learned - Software

Who is online

Users browsing this forum: No registered users and 2 guests