Objectives : compile a C/GTK+3 source code with Microsoft Visual C++ 2008, using the IDE and command line.
Many thanks to Antonio Scuri for many of the information needed to write this article.
In this tutorial, we will try to compile this C source code.
Notice : the following examples have been tested with Visual C++ 2008. You may need to adapt them for other versions.
a) Create a project
Create a new "Win32" -> "Win32 console application" project, without forgetting to check the "Empty project" option.
b) Integrate the source file
Drag-drop the "gtk3test.c" source file in the "Source files" section.
c) Add the include folders
Click on "Project" -> "Properties..." -> "Configuration properties" -> "C/C++" -> "General".
Click on the "..." button ending the "Other Include directories" line.
Click on the first button, displaying a folder ("New line"), and add the needed paths one-by-one :
(you may need to modify this regarding your GTK+ install location)
d) Add import libraries
Open Windows© Explorer, move to the GTK+ "lib" subfolder, and drag-drop all .lib files to the "Resource files" section.
(we only need these 2 ones here)
e) Add the -Dinline= option
Visual C++ does not understand the "inline" directives ; we will replace them with harmless blanks. Return to the former C/C++ option window, "Command line" subsection, and write "-Dinline="
f) Compile and run
Press the [F5] key... Here we go !
a) Run the Visual Studio Command Line
Run "Start" -> "Programs" -> "Microsoft Visual C++..." -> "Visual Studio Tools" -> "Visual Studio Command Line".
b) Put the GTK+ installation folder in a variable
Get the absolute path of your GTK+ installation folder (default : "C:\Program Files\GTK+-Bundle-3.6.1"). Then put it in a variable :
set GTKDIR=C:\Program Files\GTK+-Bundle-3.6.1
(PS : no spaces between "=" and the rest, no quotes for the path)
We will run the C compiler with, as parameters, the source file, include folders (containing .h files), the little -Dinline= trick, libraries folder (containing .lib files) and .lib files themselves :
cl gtk3test.c -I"%GTKDIR%\include\gtk-3.0" -I"%GTKDIR%\include\glib-2.0" -I"%GTKDIR%\lib\glib-2.0\include" -I"%GTKDIR%\include\pango-1.0" -I"%GTKDIR%\include\cairo" -I"%GTKDIR%\include\gdk-pixbuf-2.0" -I"%GTKDIR%\include\atk-1.0" -Dinline= /link /libpath:"%GTKDIR%\lib" gtk-win32-3.0.lib gobject-2.0.lib
(PS1 : "-Dinline=" will replace the unsupported "inline" keyword with harmless blanks)
(PS2 : we only specified "gtk-win32-3.0.lib" and "gobject-2.0.lib" because our program is very simple. A more complex code could require more libraries)
Done ! Next times, even if GTK+ install location changes, you will only need to modify GTKDIR.
Final result :