To compile code with GTK+ in Windows, the recommended toolchain is MinGW (for a variety of reasons). However, if you want to use Cygwin anyway here is some guidance, which may save you some searching time and head-scratching.
I’ll assume:
- You have Cygwin installed and you know how to use it
- You have gcc installed within Cygwin
- You know how to add entries to the system PATH safely
(Note before we start that it may be possible to so some of this from Cygwin’s installer but I found that manual setup was easier)
Step 1
Download the ‘all-in-one bundle’ from the GTK+ download page and unzip it to your preferred installation directory, e.g. C:/gtk . This is the example directory I’ll use below.
Step 2
Set your system variables:
- Add the ‘bin’ directory to your system PATH (e.g. C:/gtk/bin )
- Add a variable called GTK_BASEPATH with the value of the installation directory (e.g. C:/gtk )
- Add a variable called LIB with the value of the ‘lib’ subdirectory (e.g. C:/gtk/lib )
- Add a variable called PKG_CONFIG_PATH with the value of the pkgconfig directory (e.g. C:/gtk/lib/pkconfig )
Note that these can also be done on the Cygwin command line or in your bash profile using ‘export’, e.g.
1 2 3 |
export GTK_BASEPATH=/cygdrive/c/gtk export LIB=$GTK_BASEPATH/lib export PKG_CONFIG_PATH=$GTK_BASEPATH/lib/pkgconfig |
Step 3
Open a fresh Cygwin shell and ensure your path was set up correctly:
1 |
pkg-config --version |
This should return a version number if you completed Step 2 correctly.
Step 4
Now we need to fix the paths in the pkgconfig directory. This is the most annoying part of the process. Take a look in the pkgconfig directory (e.g. C:/gtk/lib/pkgconfig) and you will find a number of .pc files. These contain information about where includes are for particular libraries. However, the top of each file contains a ‘prefix’ of where the GTK installation directory is, and for some reason these are set by default to some mystery values. We need to correctly set the prefix for all these .pc files.
This is a pain to do by hand and you may wish to script it, but it shouldn’t take more than two minutes or so.
The first line of each file is the prefix. E.g.
1 |
prefix=/devel/target/2b4acd63a3ff1c3662e3b9b0910e896f |
This simply needs to be changed to your GTK installation directory (using a Cygwin-style path). Thankfully, the rest of the entries use the prefix as a variable so this is all we need to change. So if your installation is in C:/gtk
Then your prefix line simply looks like this:
1 |
prefix=/cygdrive/c/gtk |
Make sure to do this for all .pc files in this directory.
Step 5
Check the paths to libraries/includes are valid by typing
1 |
pkg-config --cflags --libs gtk+-2.0 |
This should return a long line of library switches for gcc to use, and these should all have /cygdrive/… in them. If this contains the original file path prefix as shown in Step 4, you may have missed a file.
This line of code allows us to delegate the library part of the gcc call to the pkg-config tool, making compilation easier.
Step 6
Let’s compile a GTK+ hello world program. Here is one I took from the GTK+ tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <gtk/gtk.h> int main(int argc, char *argv[]) { GtkWidget *window; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_show(window); gtk_main(); return 0; } |
This simply initialises a blank 200×200 window (which you must forcibly close since it does not handle close button events).
Compile with this gcc command:
1 |
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-2.0` |
If all goes well, the program should compile correctly and you should see the plain window, like this one:
If you have trouble executing the program (e.g. a shared library error), you may need to ensure you added the ‘bin’ directory to your system PATH correctly in Step 2.
Good luck!
12 Responses to “Compile GTK+ code with Cygwin Tutorial”
thanks !
for the annoying part:
cd /cygdrive/d/cygwin/gtk/lib/pkgconfig
ls *.pc|while read line;do cat $line|sed -s “s/^prefix\=\(.*\)/prefix\=\/cygdrive\/d\/cygwin\/gtk/gi” > $line;done
where “\/cygdrive\/d\/cygwin\/gtk” needs to changed as needed
for me it did the trick.
The contributed script was zeroing out several files. This worked for me, but YMMV:
for i in $(ls -1) ; do cat $i | sed -s “s/^prefix\=\(.*\)/prefix\=\/cygdrive\/c\/opt\/gtk/gi” > $i.tmp ; mv $i.tmp $i ; done
Hey, I followed these instructions but in the end “gcc hello.c -o hello
pkg-config --cflags --libs gtk+-3.0
” gives me a “no such file or directory” error. When checking with “pkg-config –cflags –libs gtk+-3.0″ the libs are listet nicely, though not with the /cygdrive/ in it, but no mysterious random path either. It just lists -IC:/gtk/include/gtk-3.0 -IC:/gtk/include/cairo -IC:/gtk/include/pango-1.0 -IC:/gtk/include/atk-1.0 … etc.What could have gone wrong?
It seem you missed the little ” ‘ ” for the pkg-config command. This is required, otherwise g++ will try to compile this options – not a good idea. xD
This was novel. I wish I could read every post, but i have to go back to work now… But I’ll return.
Well, I’ve done that like you – only with gtk+3.0. The window requires then a running xserver like Xming under windows – Do you know any solution?
Thanks,
Simonmicro
Hi. I see that you don’t update your site too often. I know that
writing posts is time consuming and boring. But did you know that there is a tool that allows you to
create new posts using existing content (from article directories or other websites from your niche)?
And it does it very well. The new articles
are high quality and pass the copyscape test.
You should try miftolo’s tools
Definitely, what a magnificent website and revealing posts, I will bookmark your website.Have an awsome day!
Mega schön
I actually wanted to compose a small comment to be able to say thanks to you for all the precious tips you are writing on this website. My rather long internet search has now been paid with high-quality concept to go over with my close friends. I ‘d state that that most of us site visitors actually are very blessed to be in a good network with very many lovely individuals with useful principles. I feel really lucky to have used the web pages and look forward to tons of more brilliant times reading here. Thanks once more for a lot of things.
Thanks , I have just been looking for information approximately this subject for a while and yours is the greatest I’ve discovered so far. However, what in regards to the bottom line? Are you sure about the supply?
Great post. I’m facing many of these issues as well..