Using gtkmm with Microsoft Visual Studio

These instructions were written by Alan Ott for Visual Studio 2005, but they apply to Visual Studio 2008 as well.

Installing and Configuring Visual Studio Express

You may wish to build and run gtkmm applications on Microsoft Windows with the free-of-cost Visual Studio Express Edition. The following instructions descibe how to configure a workstation to build gtkmm applications easily. If you already have Visual Studio Standard Edition or above, skip to the section titled Installing Gtkmm

Download Visual Studio Express from Microsoft's Visual C++ Download Page.

After installing Visual Studio, you'll need to download the Windows Platform SDK. Because Microsoft requires that you validate your Windows install using the GenuineAdvantage ActiveX control, you must download it using Internet Explorer from a Windows box with a valid license.

The Platform SDK can be downloaded from the platform SDK download page.

After installing the Platform SDK, use the instructions from Brian Johnson to configure Visual Studio to build native Win32 Applications.

Installing gtkmm

Once Visual Studio is installed and has been configured to build native Win32 applications, you must install the gtkmm development package. To do this, download and install the lastest gtkmm development package from the Gnome FTP site. Do not be afraid of the size of the development packages as they are much larger than the runtime packages which your end users will depend on.

Creating a New Project with Gtkmm Support

Getting Started

First, create a new project in Visual Studio by Selecting New Project in the File menu as shown in the figure below.


From the New Project window, make sure to select Win32 and Console Application as shown in the following figure. Also, give your project a name and a location. For this example, the name of the project is gtkmm_test and the location is C:\work.



Clicking Finish will create for you a new native Win32 console project. Now we need to modify this project to use Gtkmm.


Correct main() function

The first order of business for this new project is to correct the main() function. Visual Studio wants to use a translated main function called _tmain(). This is fine, but will give you a non-portable project, and one of the goals of gtkmm is to provide a framework for portable applications. That being said, remove _tmain() and replace it with good old fashioned main(), just like mom used to make.


Correct stdafx.h

The next thing to alter is the stdafx.h precompiled header file. At the time of this writing, many Windows programmers are familiar with the concept of precompiled headers. However, many Unix programmers are not, as precompiled header support was only recently added to GCC (in version 3.4) and is still not used in most open source projects. Unix programmers may be tempted to just disable precompiled headers altogether, but think carefully before doing this. Proper use of precompiled headers provides a much improved compile time when using gtkmm, and will save you many hours over the course of a project.

The following figure shows how to change the default stdafx.h to be more portable, and also shows the include line for the gtkmm.h header file. The portability changes include removing the #pragma once line and replacing it with a standard #ifdef include guard as well as removing the tchar.h include. It is advisable to put all of your gtkmm related headers (e.g.: libglademm.h, libxml++, etc.) in this file as opposed to other files as this will greatly speed up the compilation of your project.


Add Code to Create a Simple gtkmm Window

Next, add the contents of a simple gtkmm program to the main() function. The example shown in the figure below is the one from Chapter 3, Basics of the gtkmm tutorial.


Add the MSVC Property Files for gtkmm

The next step is to add the MSVC Property files which come with the gtkmm distribution. Since the 2005 version, Visual Studio supports the use of property files for adding settings to a project. Property files can contain build settings of all kinds (e.g.: defines, include paths, link paths, and libraries) and make it easy to build against 3rd party packages. When a property file is added to a project, the project inherits all the build settings which the property file specifies. To keep your project portable (portable in the relative path vs. fixed path sense), you will want to copy the property files from the gtkmm distribution (commonly C:\Gtk\MSVC\) to the directory which contains your project (in our case C:\work\gtkmm_test\).

Next, add the property files to your project. Do this by clicking the Property Manager tab of your Solution explorer as indicated in this figure:


Right-Click on the Debug | Win32 folder and select Add Existing Property Sheet. From the file browser, select the file gtkmm-vc80-d-2_4.vsprops. Next, Right-Click on the Release | Win32 folder and again select Add Existing Property Sheet. From the file browser, this time select the file gtkmm-vc80-2_4.vsprops. If you are using MSVC++ 2008 instead of MSVC++ 2005, then you should use property sheets with vc90 in the name instead.


When you are done, the Property Manager should look like the one in this figure:


Change a Few Project Settings

At this point we're almost done. The last part is to change a few settings in the Project Properties page. The easiest way to get to this page is to right-click on the project name in the Solution Explorer and click the Properties menu item as shown here:


The first thing to do is to remove the $(NoInherit) option from the linker settings as shown in the following figure. Get to the linker page using the tree on the left. Open Configuration Properties, then Linker, then click on Input. Now remove the $(NoInherit) token from the Additional Dependencies line. Make sure to do this in both Debug and Release modes.


At this point your application will build and run. However, The gtkmm headers will cause a harmless warning during the build. Fortunately, this warning can be disabled from the same Project Settings dialog. In the tree on the left of the dialog, open Configuration Properties, then C/C++, then click Advanced. Now type 4250 in the Disable Specific Warnings field as shown in the following screenshot. Make sure to make this change in both Release and Debug modes. Press Ok when done.


About the Windows Console

If you build and run your application now, you will see a simple Gtk+ window appear with nothing on it. Your application will also pop up a console window with it. The console window is quite valuable for debugging a gtkmm application because many Gtk+ warnings will print to this console and nowhere else (unlike the OutputDebugString() or TRACE() functions which Windows programmers are familiar with).

When you release your application however, you will probably want to have it not pop up the console. This requires two small changes to the linker settings. From the tree on the left of the properties page, open Configuration Properties, then Linker, then System. Now change the value of the Subsystem field from CONSOLE to WINDOWS as shown in the figure below. This will disable the creation of the console window when your app starts. When you do this, you also have to change the entry point of your application. From the tree on the left, select Advanced then change the Entry Point field to read mainCRTStartup (make sure to use the proper case) as shown in the second screenshot below.

Some users have found it convenient to leave the console active in Debug mode and disabled in Release mode. Others will want to have it the same in both modes. It is completely up to the preference of the developer. Press Ok when done.



That's all there is to it. After this initial setup, your gtkmm app can be edited, built, and run just like any other Visual Studio project.

Attic/GtkmmWithMicrosoftVisualStudio (last edited 2015-05-25 12:29:45 by SvitozarCherepii)