午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

使用Visual C++編譯GALib(靜態(tài)庫)

 jtll521 2012-05-07

另外再多說一句:經過我的多次嘗試,發(fā)現(xiàn)galib能夠編譯成dll,在它提供的makefile中提供了編譯dll的一些參數(shù),經過一些簡單的修改,就可以生成dll,但是生成的dll沒有相應的lib(輸入庫),后來進過一番查證才發(fā)現(xiàn),生成dll中沒有一個導出函數(shù),也就是說galib的源碼沒有提供API導入導出的功能(雖然在"gaconfig.h”提供了"COMPILE_GALIB_AS_DLL”和"USE_GALIB_AS_DLL”兩個宏)

另外向高人求教兩個問題:

1、為什么有時候使用相同的編譯參數(shù)在vc的命令行中出現(xiàn)一大堆莫名其妙的錯誤,而在VC IDE中則編譯正常呢?(所謂的相同基本上就是把VC IDE配置好的工程的那些編譯參數(shù)拷出來,然后用到vc 編譯器的命令行上

2、為什么galib編譯出來的release版和debug版大小相差那么懸殊呢?(在我的機器上,ga.lib(17.9M,release),gad.lib(2.15M,debug))已解決★★★

問題2解決方法:在release版,工程“屬性”->“配置屬性”->“ C/C++ ”->“優(yōu)化”->“全程序優(yōu)化”選擇“否”(默認為“是”),這樣設置后編譯出來的ga.lib尺寸為1.83M(release)

GALib is a software implementation of various 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.

(這段和下一段就不翻譯了,簡單說一下,GALib是有MIT開發(fā)的一個c++遺傳算法庫,它是基于BSD協(xié)議的,也就是說我們可以免費的使用它,而不用承擔什么責任,比GPL、LGPL等協(xié)議更加的“自由”,可惜好像停止開發(fā)了,最新版本為2.47,不過基本上功能齊全。搜索到這個頁面的,估計都是學習過遺傳算法的基本知識的,別的廢話就不多說了)

Note: These instructions should be applicable to the Visual C++ shipped with the express, standard and professional versions of Visual Studio.

另外GALib本身也提供了makefile,如果自認為makefile學的還可以的,可以參見galib目錄下的readme、makefile.vcpp、makevars.vcpp以及ga目錄下的makefile.vcpp、makefile.sources這幾個文件(這里只關注windows下的編譯過程)。OK,開始干活了!

Prerequisites

準備

下載GALib(最新版2.47,下載地址:GALib website

安裝vc2008/vc2005(express、standard、professional版本均可,其中express版是免費的,可以從Microsoft website下載,目前最新版為VC2008 express sp1)

Step 1: Replace the .C extension on source files with .cpp

第一步:將源文件的文件名后綴由“ .c ”修改成“ .cpp ”(可選)

(這一步我感覺可有可無,因為只需要在vc編譯中加入編譯選項“ /TP ” ,就能保證vc編譯器將.c文件編譯成c++代碼,參見:工程“屬性”->“配置屬性”->“ C/C++ ”->“高級”->“編譯為”,默認選項為編譯為C++代碼/TP)

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.

打開命令行(“開始菜單”->“運行”輸入cmd),更換路徑到galib所在路徑下的ga目錄(不會的可以使用“ cd/? ”查看cd命令幫助,或者到網上搜索一下);例如,galib的路徑為“ D:\program\galib247 ”,那么在命令行中輸入“ d: ” ->“ cd d:\program\galib247 ”->“ cd ga ”(這里的過程略微繁瑣一些,主要是照顧那些dos命令不熟悉的人,如果高手就無視了哈)

運行如下的命令:

rename *.c *.cpp

Step 2: Create a Visual C++ Solution

第2步:創(chuàng)建vc的解決方案(solution)

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

打開Visual C++ 2008,新建工程

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:\galib246.

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 node in the solution explorer should now read Solution 'vcpp2005' (1 project). The project ga should also be shown directly below this with three empty nodes: Header Files, Resource Files, and Source Files.

選擇“常規(guī)”->“空項目”,輸入工程名稱(例如ga),輸入或選擇解決方案的位置Location(例如“ d:\program\ ”),輸入解決方案名稱(例如“ galib247 ”),保證“創(chuàng)建解決方案的目錄”復選框是選上的,這樣一個空的解決方案就創(chuàng)建完成了

ps:其實我們也可以稍微偷一下懶,新建一個“ win32項目 ”(并不是“空項目”),然后“應用程序類型”選擇“靜態(tài)庫”(不使用預編譯頭),這樣我們就可以使用模板中的一些默認的參數(shù)(如“預處理器定義”),而不需要自己手動的填入這些參數(shù)了(個人強烈推薦!??!)

Step 3: Importing GALib source and header files to the project

第3步:導入GALib的源文件和頭文件到工程中

Right-click on the Header Files node and select Add > Existing Item.... 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 the files to only header files. Select all the files using Ctrl+A and then click Add.

在“解決方案資源管理器”的工程中,右鍵“頭文件”節(jié)點->“添加”->“現(xiàn)有項”,彈出“添加現(xiàn)有項”對話框,瀏覽GALib路徑下的ga目錄(在本文的例子中為“ d:\program\galib247\ga ”),在“對象名稱”編輯框中輸入“ *.h ”,然后回車,全選所有的.h文件并添加。

Right-click on the Source Files node and select Add > Existing Item.... In the dialog box that opens, browse to the ga directory within the GALib distribution on your system. Enter *.cpp to filter the files to only source files. Select all the files using Ctrl+A and click Add.

同上,右鍵“源文件”節(jié)點->“添加”->“現(xiàn)有項”,彈出“添加現(xiàn)有項”對話框,瀏覽GALib路徑下的ga目錄(在本文的例子中為“ d:\program\galib247\ga ”),在“對象名稱”編輯框中輸入“ *.c ”,然后回車,全選所有的.c文件并添加。

Step 4: Update source code with new file names

第4步:更新源碼中的include的源文件名稱(可選)

(因為在有的文件中包含了.c文件,例如“ #include xxx.c ”。這一步也是可有可無的;同上,只需要添加編譯選項“ /TP ”——編譯為C++代碼,就不需要修改源文件中include的文件名稱了)

Because we renamed a number of files in Step 1, we also need to replace any references to these 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 origninal text with the new text:

由于我們在第1步,對文件進行了改名(將.c改名為.cpp),我們同樣需要將源碼中include的源文件改名。在vc2008 IDE中,選擇“編輯”菜單->“查找和替換”->“在文件中替換”,根據下表中的內容進行替換

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++ practices:

下面的文件替換不是必須的,主要是為了符合C++標準規(guī)范

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: Setting the ga project properties

第5步:設置工程屬性(這是重點★★★★★

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)

常規(guī)(“配置屬性”->“常規(guī)”)

配置類型 = 靜態(tài)庫(如果按照我上面說的新建一個win32 項目--靜態(tài)庫,則配置類型默認為靜態(tài)庫)

公共語言運行時支持 = 無公共語言運行時支持(這是默認選項)

C/C++->常規(guī)(“配置屬性”->“ C/C++ ”->“常規(guī)”)

附加包含目錄 = .. (表示當前工程目錄的上一層目錄,在本文中,解決方案GALib247位于 “ d:\program\galib247 ”,ga工程位于“ d:\program\galib247\ga ”,galib的源碼和頭文件位于“ d:\program\galib247\ga ”下,在這里我純粹是為了簡單,將解決方案和工程直接建在galib的源文件目錄中,希望不會把大家弄糊涂哈

C/C++->預處理器(“配置屬性”->“ C/C++ ”->“預處理器”)

預處理器定義 = _CRT_SECURE_NO_DEPRECATE; COMPILE_GALIB_AS_LIB (這2個預處理器符號可以在makevars.vcpp中找到)

C/C++->預編譯頭(“配置屬性”->“ C/C++ ”->“預編譯頭”)

創(chuàng)建/使用預編譯頭 = 不使用預編譯頭(這是默認選項)

C/C++->高級(“配置屬性”->“ C/C++ ”->“高級”)

編譯為 = 編譯為C++代碼/TP (★★★★★

注意:“ /TP ”選項有一個小bug,如果大家按照我上面說的操作,不修改文件名稱(即不將.c修改為.cpp),那么在導入頭文件(.h)和源文件(.C)到工程中后,“ /TP ”是默認選項;如果大家直接開始編譯的話,那么會出現(xiàn)一大堆的錯誤,我猜測是因為編譯器仍然是將C文件當做C代碼來編譯,而不是當做C++代碼來編譯,盡管編譯器默認選擇了“ /TP ”編譯選項

解決辦法:將“配置屬性”->“ C/C++ ”->“高級”->“編譯為”設置為其它的選項(例如“編譯為C代碼/TC”),然后再設置為“編譯為C++代碼/TP ”,再編譯就不會有錯誤了

剩下的就是編譯release版和debug版的galib的靜態(tài)庫了,這個應該不要我多說了吧。。。

下面的內容主要是講如何編譯運行galib自帶的example(這個比較簡單,就不多說了,將就著英文看吧)

主要是一下兩點:

1、設置galib的include和lib環(huán)境變量,讓編譯器知道在哪里找galib的頭文件和lib

2、編譯example的時候注意example和galib的運行時編譯選項應該是一致的,也就是說如果編譯galib使用的是/MT(release),那么example的運行時編譯選項也應該是/MT(release);如果編譯galib使用的是/MD(release),那么example對應的也應該是/MD(release)

Step 6: Build the ga project

The default build configuration in MS 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: Adding a GALib example project

GALib provides over twenty examples and the following shows how to import one of these as projects into 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: Importing the GALib example source file

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: Setting the example project properties

Select the ex1 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.

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: Setting the example project dependencies

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 the example project

Select Build ex1 from the Build menu.

Step 12: Running the example project

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 Debug 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 both GA library and Ex1 properties to the same Runtime library type (in my case, Multi-threaded Debug (/MTd))
  2. In Ex1 project properties > Linker > Input set Ignore specific library to MSVCRTD.lib;msvcprtd.lib

External Links

The GALib Mailing List
http://mailman./mailman/listinfo/galib

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多