How to configure gtk on Visual studio 2010

I have tried configuring gtk+ on visual studio but doesn't work properly., Can anyone suggest me with a proper solution, as how to install gtk on Visual studio 2010


I got GTK+ working with VS2010, so if you want to get it working too, get ready for some fun, because this will take a few minutes.

First of all, download the latest Windows All-In-One bundle. Optional direct download link for the GTK 2.24 bundle that I used here. The direct link is to the 32bit version. I have not tested the 64bit version because it is still listed as experimental.

Once you have the bundle downloaded, unzip it into something like C:\gtk or in my case D:\gtk

Next we will create a System Environment Variable for the GTK folder. Open up a command prompt and write: setx GTKDIR {Path to your GTK folder} /m which in my case would be setx GTKDIR D:\gtk /m

We are also going to add the .dll files required for GTK+ built applications to run on Windows into our system PATH. To make things very easy, I suggest you edit your system PATH with PathEditor. Now add the path to the GDK binaries folder which in my case is D:\gtk\bin to the system PATH. Confirm the GTK bin folder has been added to the PATH by typing PATH into your command prompt.

Now we move on to Visual Studio 2010 and create a new project.

File
  ->New
    ->Project
    Visual C++
      ->Win32
        ->Win32 Console Application

Then the Application Wizard Appears.

Click to select:

Windows Application

Empty Project

click Finish to proceed.

Before we add any source files, right click on the project name in the Solution Explorer and click on Properties. Now go to Configuration Properties and then VC++ Directories. We now need to add the include and library files from GTK to the Include Directories and Library Directories.

You should have the following in your Include Directories

$(GTKDIR)\lib\gtk-2.0\include
$(GTKDIR)\lib\glib-2.0\include
$(GTKDIR)\include

and Library Directories:

$(GTKDIR)\lib

While we are still in the view of the Project Properties, click on Linker and then System. Look for SubSystem on the right and click the drop down box. Select Windows /SUBSYSTEM:WINDOWS

Next up, we have to generate the flags for the compiler and the linker. Luckily, GTK+ comes with a nice little tool called pkg-config that we will use to automatically generate these flags for us. The pkg-config tool can be found in the bin folder of GTK. In my case this is D:\gtk\bin or %GTKDIR%\bin using our system variable that we defined earlier. Simply navigate to the bin folder(the created text files will be output there) using the command prompt and run the following:

pkg-config --cflags gtk+-2.0 --msvc-syntax > compilerflags.txt

This will create the compiler flags we need and store them in a text file. My Result for compiler flags (I have removed the flag -mms-bitfields, this is a gcc only flag that we don't need):

-ID:/gtk/include/gtk-2.0 -ID:/gtk/lib/gtk-2.0/include -ID:/gtk/include/atk-1.0 -ID:/gtk/include/cairo -ID:/gtk/include/gdk-pixbuf-2.0 -ID:/gtk/include/pango-1.0 -ID:/gtk/include/glib-2.0 -ID:/gtk/lib/glib-2.0/include -ID:/gtk/include -ID:/gtk/include/freetype2 -ID:/gtk/include/libpng14

We will do the same for the linker flags:

pkg-config --libs gtk+-2.0 --msvc-syntax > linkerflags.txt

My Result for linker flags:

/libpath:D:/gtk/lib gtk-win32-2.0.lib gdk-win32-2.0.lib atk-1.0.lib gio-2.0.lib pangowin32-1.0.lib gdi32.lib pangocairo-1.0.lib gdk_pixbuf-2.0.lib pango-1.0.lib cairo.lib gobject-2.0.lib gmodule-2.0.lib gthread-2.0.lib glib-2.0.lib intl.lib

With all the needed flags generated, we need to add them to our project. Once again, right click on the project name and click on Properties. Now go to C/C++ and click on Command Line. To the right you should see an empty box called Additional Options. Copy and paste the compilerflags.txt content into this box.

After finishing the above, click on Linker and then Command Line. Once again, simply copy and paste the contents of the linkerflags.txt file into the Additional Options box. While we are here, add one last linker flag /ENTRY:mainCRTStartup This flag tells Visual Studio that we want to use the standard main() rather than Microsoft's _tmain() as our main program entry point.

Finally, in the Source Files folder, create and add a new .cpp file with the following:

#include <gtk-2.0\gtk\gtk.h>

int main(int argc, char* argv[])
{
    gtk_init(&argc, &argv);

    GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_usize(window, 300, 200);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);   
    gtk_window_set_title(GTK_WINDOW(window), "GTK+ with VS2010");

    gtk_widget_show(window);     

    gtk_main();   
    return 0;
}

Everything should now be ready to compile, link and run. If all went well, you should be greeted by the following: enter image description here

Well that was fun, right? :)


If you're building GTK+ stack on Windows with the help of a guide like this, then the luxuries of using a precompiled binary will not be at your disposal.

In this case I would suggest the following approach.

  1. Download the precompiled binaries - This has two advantages.

    • This ships with the pkg-config.exe utility which you could use for the compiled source.
    • This also contains the pkgconfig folder with a wealth of .pc files which can be adapted for the compiled source.
  2. Compile the packages in the debug/release mode - well this is the main advantage of compiling it yourself - and systematically arrange the headers, libs and dlls/exe in include, lib and bin folder respectively.

  3. Copy the pkgconfig folder from the precompiled_gtk_source\bin to compiler_gtk_source\bin and set the path of the PKG_CONFIG_PATH variable to add the compiler_gtk_source\bin\pkgconfig to it.

  4. Now, considering the fact the names of the libraries produced on compiling gtk yourself and the corresponding library names in the precompiled package may be different, you might have to make the necessary changes in compiler_gtk_source\bin\pkgconfig*.pc files. I would go for a top-to-bottom approach here( We will see the advantages shortly). By top-to-bottom, I simply mean that the end product will be the one to be edited first.

    For example in this case the gtk+ is the end product and I will go for configuring the .pc of this package first. The procedure is as follows:

    • First look at the name of the dll created. In my case it is gtk-3.0. If the .pc file that was shipped with the precompiled binaries have another name, then change the name appropriately - in my case it is gtk-3.0.pc. (This should be the case with all the other .pc files.)
    • Open the gtk-3.0.pc in a text editor and you will see stuff like below.

gtk-3.0.pc file:

    prefix=c:/gtk_compilation/vs12/win32
    exec_prefix=${prefix}
    libdir=${exec_prefix}/lib
    includedir=${prefix}/include
    targets=win32

    Name: GTK+
    Description: GTK+ Graphical UI Library
    Version: 3.18.2
    Requires: gdk-3.0 atk-1.0 cairo cairo-gobject gdk_pixbuf-2.0 gio-2.0
    Requires.private: atk
    Libs: -L${libdir} -lgtk-3 
    Cflags: -I${includedir}/gtk-3.0 -mms-bitfields

The one above is my customized file. If you don't know what each line does, you could have a look at pkg-config help. The Require option, however, needs a special mention. It is the place where you put the dependancy dlls - again make sure that the name of the dlls exactly matches what you have in your bin folder.

  1. We need to recursively change the .pc files for the dlls which are included after the Require statement and if any .pcs are missing or if there is a name mismatch, you could make the appropriate changes. By now, you should already have understood the advantage of top-to-bottom approach. It helps us to sort out dependency .pc files systematically until we sort out all of them. Now, run the below command to see if things are working.

  2. Finally run the pkg-config command like below :

    pkg-config --cflags gtk-3.0 --msvc-syntax > compilerflags.txt pkg-config --libs gtk+-2.0 --msvc-syntax > linkerflags.txt

I just redirected the results to a text file named compilerflags.txt & linkerflags.txt so that you could use them any time you want by cut,copy & paste. Ofcourse, you should retain only the compiler specific flags.

Hope this helps somebody, sometime.