7

I have managed to compile a gtk+ application with cygwin, but unfortunately with this approach, the application needs x windows running to be able to startup.

How can I compile my gtk+ application to run natively on windows.

I have seen various posts online about using the -mno-cygwin flag to gcc, but that seems to have been deprecated?

I have also seen these posts on stackoverflow, but its not clear if they are trying to compile for X, or natively for Windows:

Community
  • 1
  • 1
Chris Snow
  • 23,813
  • 35
  • 144
  • 309

1 Answers1

9

The application needs to be compiled using MinGW and not Cygwin.

The full list of steps I followed:

1) Download MinGW

2) Install MinGW to a folder without spaces, e.g. to c:\MinGW.

3) Download gtk+. Even though my machine is 64bit, I went for the 32 bit download of gtk+ because compatibility warnings on the 64bit download page. The GTK+ Win32 downloads are here. I went for the all-in-one version.

4) Extract gtk+ to a folder without spaces, e.g. c:\gtk

5) If you don't already have an application, you can use the gtk+ hello world source code. Save this to a folder, e.g. c:\myapp\

6) Open a windows command prompt and cd to the folder in step 5. E.g.

cd c:\myapp

7) In the command window, add your MinGW folder to the windows PATH, e.g.

c:\myapp> set PATH=c:\gtk\bin;%PATH%

8) In the command window, add your gtk+ folder to the windows PATH, e.g.

c:\myapp> set PATH=c:\gtk\bin;%PATH%

9) Create a script to compile your application, e.g.

C:\myapp> C:\MinGW\msys\1.0\bin\bash.exe -c "echo gcc -Wall -g helloworld.c -o helloworld `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0` > compile.bat"

Note that I had to give the full path to bash.exe. For some reason adding c:\MinGW\msys\1.0\bin to the PATH and just using bash.exe didn't work for me.

10) compile your application with compile.bat, e.g.

c:\myapp> compile.bat

12) execute your application, e.g.

c:\myapp> helloworld.exe

screenshot


EDIT:

For step 9, we are just creating a gcc command to compile gtk+ with the correct include and library option settings.

This is the contents of compile.bat that got generated for me:

gcc -Wall -g helloworld.c -o helloworld -mms-bitfields -Ic:/DEV/gtk224/include/gtk-2.0 -Ic:/DEV/gtk224/lib/gtk-2.0/include -Ic:/DEV/gtk224/include/atk-1.0 -Ic:/DEV/gtk224/include/cairo -Ic:/DEV/gtk224/include/gdk-pixbuf-2.0 -Ic:/DEV/gtk224/include/pango-1.0 -Ic:/DEV/gtk224/include/glib-2.0 -Ic:/DEV/gtk224/lib/glib-2.0/include -Ic:/DEV/gtk224/include -Ic:/DEV/gtk224/include/freetype2 -Ic:/DEV/gtk224/include/libpng14 -Lc:/DEV/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl

Of which, the include options created by pkg-config --cflags gtk+-2.0:

-mms-bitfields -Ic:/DEV/gtk224/include/gtk-2.0 -Ic:/DEV/gtk224/lib/gtk-2.0/include 
   -Ic:/DEV/gtk224/include/atk-1.0 -Ic:/DEV/gtk224/include/cairo 
   -Ic:/DEV/gtk224/include/gdk-pixbuf-2.0 -Ic:/DEV/gtk224/include/pango-1.0 
   -Ic:/DEV/gtk224/include/glib-2.0 -Ic:/DEV/gtk224/lib/glib-2.0/include 
   -Ic:/DEV/gtk224/include -Ic:/DEV/gtk224/include/freetype2 
   -Ic:/DEV/gtk224/include/libpng14

(note I have put in line breaks above to improve readability on stackoverflow)

Notice that pkg-config --cflags gtk+-2.0 has put the full path of my gtk+ include files (c:/DEV/gtk224/include/).

And the library options generated by pkg-config --libs gtk+-2.0:

-Lc:/DEV/gtk224/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 
   -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpango-1.0 
   -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl

(note I have put in line breaks above to improve readability on stackoverflow)

Notice that pkg-config --libs gtk+-2.0 has put the full path of my gtk library folder (c:/DEV/gtk224/lib).

For more information on pkg-config see the GTK+ documentation

Chris Snow
  • 23,813
  • 35
  • 144
  • 309
  • For step 9, just use a Makefile. And use mingw-get to install MSYS and be able to uses UNIX-like paths. – liberforce Sep 14 '13 at 14:01
  • Hi, Chris on my system bash.exe file is missing from bin folder (actually MinGW folder don't have msys folder). This is my compile.bat code echo gcc -Wall -g helloworld.c -o helloworld `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`. But it just gets printed two times, when I execute it from cmd prompt. You can find my question at http://stackoverflow.com/questions/19325552/compiling-and-running-gtk-application-on-windows-7 – msinfo Oct 11 '13 at 22:49
  • @msinfo I have added some more information to my post that shows what step 9 is actually doing. Hopefully this will help you to create your own gcc command. – Chris Snow Oct 12 '13 at 02:59
  • Thanks Chris. It's has been mistake from my side. How fool I was to think that bash.exe was some another executable in MinGW folder. It later occurred to me that it's windows native bash.exe. Thanks for step wise comments on my original post. – msinfo Oct 12 '13 at 23:32
  • Can anyone confirm whether the above still works in 2021? I will soon have to find out, so it may be helpful if someone could confirm that; the above is almost 8 years old by now ... – shevy Feb 07 '21 at 11:34