You are here

Tutorial : compile a C/GTK+3 program with Microsoft Visual C++

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.

Although the GTK+ 3.6.1 Bundle installer provided on my blog is mostly MinGW-oriented, it is perfectly feasible to use it with Microsoft Visual C++ (by following some guidelines).

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.

1) Graphically in the IDE

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 !

1) Using the command line

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)

c) Compile

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 :


frameImage = gdk_pixbuf_new_from_file("c:\\home\\ABC\\Image_1.jpg",&error);
gdk_pixbuf_save_to_buffer(frameImage,(gchar **)&ImageData,&Datasize,"bmp",&error);

The above code fragment is added to above sample code, it compiles and runs without any exception.
In release mode the runtime exception is 0xC0000005: Access violation reading location 0x00000001.

(originally posted by Rupesh)

Hi Rupesh,

I'm by no means a VC++ expert, but it's possible that its "Release" mode expects some tests to be done (and so, warnings to be suppressed) before allowing some functions to run.
Here for instance, you don't test the existence of the file before using it. Have you looked at functions like g_file_query_exists () ?

PS : if it's not the cause, then I'm clueless.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.