2010/03/08 | Microsoft Visual C++ 编译 GAlib
类别(学术资料) | 评论(0) | 阅读(855) | 发表于 10:23

GAlib 是一个遗传算法库,属于人工智能的范畴,采用自然进化和选择的原则解决负责问题。本页是在Microsoft Visual C++ 开发平台编译 GAlib 库的简要步骤。

基本配备

GAlib version 2.4.7
到这里下载。GAlib website 
Microsoft Visual C++
Visual C++ 2005 或 2008。到这里下载。Microsoft website

Step 1: 修改文件后缀

运行命令行提示符(开始> 运行> cmd.exe) ,进入GAlib 解压后的ga目录。

运行下面命令:

$ rename *.C *.cpp

进入 examples 目录运行相同命令。

Step 2: 新建解决方案和工程

打开 Microsoft Visual C++,文件菜单下选择新建 > 项目。

对话框左边的项目类型选择 Visual C++ 下的常规。

对话框右边的模板选择空项目。

输入项目名称。本例中,项目名称为 ga。

输入或浏览项目位置。本例假设位置在GAlib 解压目录下的子文件夹 projects。例如, C:\galib247\projects

输入解决方案名称,例如 vcpp2005 。选择创建解决方案的目录。

创建解决方案后,解决方案资源管理器中顶项显示为解决方案 'vcpp2005' (1 项目)。ga 项目显示在其下,包括三个子项: 头文件,源文件,资源文件。

Step 3: 导入源文件和头文件

右击项目ga,选择添加 > 现有项...。浏览至ga目录,选择全部文件然后单击添加。

Step 4: 修改源码中引用的文件名

因为第一步 step 1 重命名了一些文件,所以需要替换源码中对这些文件的引用。选择在文件中替换,用每一行中新文本替换原始文本,需要替换的内容见下表

需要替换的文本
原始文本新文本
<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>

以下替换不必需但符合标准 C++:

可选替换文本
原始文本新文本
<stdio.h><cstdio>
<stdlib.h><cstdlib>
<assert.h><cassert>
<limits.h><climits>
<time.h><ctime>
<math.h><cmath>
<string.h><cstring>

Step 5: 设置项目属性

设置“配置属性”以下项:

常规
配置类型 = 静态库(.lib)
公共语言运行库支持 = 公共语言运行库支持
C/C++ > 常规
附加包含目录 = ..\..\..
C/C++ > 预处理器
预处理器定义 = _CRT_SECURE_NO_DEPRECATE
预编译头
创建/使用预编译头 = 不使用预编译头
高级
编译为 = 编译为 C++ 代码(/TP)

Step 6: 生成项目

Visual C++ 默认解决方案配置为 Debug,若不需要调试 GAlib 代码,将解决方案配置改为Release。

选择生成菜单下的生成 ga 生成项目。

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
不二
翻译时间:
             2010年3月8日

 

0

评论Comments

日志分类
首页[64]
我的日志[31]
VTK[2]
QT[2]
计算机FAQ[6]
VC & C++[7]
学术资料[4]
Ubuntu[4]
网易评论[1]
恢复视力[5]
个人提升[1]
PHP+MYSQL[1]