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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With