Compiling GAlib Using Microsoft Visual C++

GAlib is a software development library for Evolutionary Algorithms, a branch of Artificial Intelligence concerned with finding solutions to complex problems using the principles of natural evolution and selection. This page gives a brief summary of the steps required to compile the GAlib library and the provided examples using the Microsoft Visual C++ development platform.

Prerequisites

GAlib version 2.4.7
Available for download from the GAlib website.
Microsoft Visual C++
The Express edition is available to download for free from the Microsoft website. This tutorial was written for the 2005 version but it should be applicable to the current 2008 edition. It should also be applicable to the versions of Visual C++ that are shipped with the Express, Standard, and Professional versions of Microsoft Visual Studio.

Step 1: Modify the Filename Extensions

Open a command window (Start > Run > cmd.exe) and change to the ga directory within the GAlib distribution.

Run the following command:

$ rename *.C *.cpp

Change to the examples directory and run the same command.

Step 2: Create a New Solution and Project

Open Microsoft Visual C++ and select New > Project from the File menu.

Select General under the Visual C++ node in the list of Project types on the left of this dialog.

Select Empty Project in the templates panel on the right.

Enter a name for the project. In this example, the project name is ga.

Enter or browse to a location for the project. This example assumes the location to be in the subdirectory projects within the GAlib distribution directory e.g. C:\galib247\projects .

Enter a name for the solution e.g. vcpp2005 and check the Create directory for solution check box.

Once you have created the solution, the top entry in the Solution Explorer should now show Solution 'vcpp2005' (1 project). The ga project should also be shown directly below this with three empty sub-entries: Header Files, Resource Files, and Source Files.

Step 3: Import the Source and Header Files

Right-click on the Header Files node and select Add > Existing Item... in the pop-up menu. In the dialog box that opens, browse to the ga directory within the GAlib distribution on your system. Enter *.h into the File name text box and press Enter. This will filter non-header files out of the list. Select all the files using Ctrl+A and then click Add.

Right-click on the Source Files node and select Add > Existing Item... in the pop-up menu. In the dialog box that opens, browse to the ga directory within the GAlib distribution on your system. Enter *.cpp and press 'Enter' to filter non-source files out of the list. Select all the files using Ctrl+A and click Add.

Step 4: Modify Filename References in the Source Code

Because we renamed a number of files in step 1, we also need to replace any references to those files within the GAlib source code itself. Select Replace in files.. from the Edit menu and select Entire Solution in the Look in: combo box in the dialog that opens. For each row in the following table, replace the original text with the new text:

Required text replacements
Original Text New Text
<ga/gatree.c> <ga/gatree.cpp>
<ga/gaallele.c> <ga/gaallele.cpp>
<ga/galist.c> <ga/galist.cpp>
<ga/gatreegenome.c> <ga/gatreegenome.cpp>
<ga/ga1darraygenome.c> <ga/ga1darraygenome.cpp>
<ga/ga3darraygenome.c> <ga/ga3darraygenome.cpp>
<ga/ga2darraygenome.c> <ga/ga2darraygenome.cpp>
<ga/galistgenome.c> <ga/galistgenome.cpp>

The following replacements are not essential but conform to standard C++ practice:

Optional text replacements
Original Text New Text
<stdio.h> <cstdio>
<stdlib.h> <cstdlib>
<assert.h> <cassert>
<limits.h> <climits>
<time.h> <ctime>
<math.h> <cmath>
<string.h> <cstring>

Step 5: Set the Project Properties

Select the ga project in the Solution Explorer and click Properties from the Project menu.

Set the active configuration to All Configurations in the combo box on the top left of this dialog and set the following Configuration Properties:

General
Configuration Type = Static Library (.lib)
Common Language Runtime Support = No Common Language Runtime Support
C/C++ > General
Additional Include Directories = ..\..\..
C/C++ > Preprocessor
Preprocessor Definitions = _CRT_SECURE_NO_DEPRECATE
Precompiled Headers
Create/Use Precompiled Headers = Not Using Precompiled Headers
Advanced
Compile As = Compile As C++ Code (/TP)

Step 6: Build the Project

The default build configuration in Visual C++ has debugging enabled. If you don't want to debug the GAlib code, select Configuration Manager... from the Build menu and change the configuration to Release in the active solution configuration combo box on the top left of the dialog.

Select Build ga from the Build menu to build the project.

Step 7: Add a New Project for the Example

GAlib provides over 20 examples and the following shows how to import one of these into a new project in the solution.

Select Add > New Project... from the File menu. Select Visual C++ > Win32 in the Project types list on the left.

Select Win32 Console Application in the Templates list on the right.

Enter a project name. This example uses the project name ex1.

In the wizard that opens, click Next to skip the first page.

Check Empty Project in the Additional options group and un-check the Precompiled header check box.

Click Finish to close the wizard and add the project to the solution.

Step 8: Import the Example's Source Files

Right click on the Source Files node under the ex1 project node and click Add > Existing Item....

Browse to the examples directory in the GAlib distribution and select ex1.cpp. Click Add.

Step 9: Set the Project Properties for the Example

Select the ex1 project node in the Solution Explorer and click Properties from the Project menu.

Set the active configuration to All Configurations in the combo box on the top left of this dialog and set the following property values:

General
Configuration Type = Application (.exe)
Common Language Runtime Support = No Common Language Runtime Support
C/C++ > General
Additional Include Directories = ..\..\..
C/C++ > Code Generation
Runtime Library = Multi-threaded (/MT)
C/C++ > Preprocessor
Preprocessor Definitions = _CRT_SECURE_NO_DEPRECATE
Precompiled Headers
Create/Use Precompiled Headers = Not Using Precompiled Headers
Advanced
Compile As = Compile As C++ Code (/TP)

Step 10: Set the Project Dependencies for the Example

Select the ex1 project in the Solution Explorer. Select Project Dependencies... from the Project menu.

Select ex1 in the Projects: combo box at the top of the Dependencies tab page.

Check the ga project in the Depend on: list and click OK.

Step 11: Build and Run the Example

Select Build ex1 from the Build menu.

Set the example project as the startup project of the solution by selecting the ex1 project node in the Solution Explorer and selecting Set As Startup Project in the Project menu.

To run the example, select Start Without Debugging... in the Debug menu. To run in debugging mode, select Start Debugging... in the same menu.

Linking Problems

One reader has posted some additional comments relating to errors he experienced when linking the example executable with the ga library. These errors were of the form:

Error1error LNK2005: public: void __thiscall std::basic_ios
>::clear(int,bool)(?clear@?$basic_ios@DU?
$char_traits@D@std@@@std@@QAEXH_N@Z)
already defined in ex1.objmsvcprtd.lib

The following steps solved this problem for the reader:

  1. In Project Properties > Code Generation set the runtime library type in both ga library and ex1 to the same value. In this example, the value Multi-threaded Debug (/MTd) was used.
  2. In ex1 > Project Properties > Linker > Input, set Ignore specific library to MSVCRTD.lib;msvcprtd.lib.
The GAlib Mailing List