在SDK/angelscript/projects目錄中,您可以找到許多流行編譯器的相關(guān)項(xiàng)目文件(原文: In the sdk/angelscript/projects directory you'll find project files for many of the popular compilers)。不過(guò),那些工程文件不一定是最新的腳本庫(kù)工程。(原文: these project files are not always up to date with the latest version of the library.)如果你有任何一個(gè)編譯或鏈接出錯(cuò),請(qǐng)弄清楚工程文件包含的所有文件所在的sdk/angelscript/source目錄(原文: If you get any compiler or linker errors please make sure the project file include all the files in the sdk/angelscript/source directory),并將工程按照本文件進(jìn)行設(shè)置(原文: and that the project settings are set according to this article.)
,
如果你找不到符合你編譯器的工程文件,你可以通過(guò)添加sdk/angelscript/source目錄下的全部文件到你的工程,并且對(duì)項(xiàng)目作適當(dāng)?shù)呐渲茫ㄔ?/span>: If you don't find a project file for your compiler, you can easily create your own project by adding all the files in the sdk/angelscript/source directory, and configuring the project apropriately.)。
如果你有一個(gè)從沒(méi)使用過(guò)AngelScript的新編譯器,你可能需要編輯as_config.h文件以便合理的編譯本工程(If you have a new compiler/target that hasn't been used with AngelScript before, you may need to edit the as_config.h file to make sure the library is compiled properly. )。
1.1.1. 編譯時(shí)的選項(xiàng)設(shè)置(Set compile time options)
創(chuàng)建as_config.h頭文件是為了使本代碼盡可能的包含不同的編譯環(huán)境(原文: The code tries to contain compiler differences in as few places as possible. The header as_config.h was created for that purpose.)。為了支持不同的編譯器,你可能會(huì)發(fā)現(xiàn)一些 #defines 宏定義(There you will find some #defines that allow different compilers to work.)。這個(gè)文件一般情況下不需要修改,但是如果你用一個(gè)過(guò)舊的編譯器編譯出錯(cuò)的話,可能就需要看一下這個(gè)文件了(原文: You'll probably not have to change this file, but if you're using a compiler not previously used and you're getting compiler errors it might be worth it to take a look at this file.)。
【當(dāng)在編譯這個(gè)庫(kù)時(shí)有一些 #defines 宏定義需要改寫,比如寫你應(yīng)該定義一個(gè)ANGELSCRIPT_EXPORT宏以便導(dǎo)出庫(kù)函數(shù)(原文: There are also a couple of other #defines used in the code to alter the compilation. When compiling the library you might want to define ANGELSCRIPT_EXPORT so that library functions are exported.)。】當(dāng)然,如果在你的工程中包含了庫(kù)源代碼目錄的話就不用定義這個(gè)標(biāo)記了(If you include the library source code directly in your application project you shouldn't have to define this flag. )。
如果定義了AS_DEPRECATED 則支持向后兼容,這就可以幫你更方便的更新到最新版本(If AS_DEPRECATED is defined then some backwards compatibility is maintained, this can help you do the upgrade to the latest version a little more smoothly.)。盡管不能保證支持向后兼容,但是應(yīng)盡可能的移除不支持的功能(There is no guarantee that the backwards compatibility will be maintained though so try to remove use of deprecated functions as soon as possible.)。
1.1.2. 關(guān)聯(lián)腳本庫(kù)(Linking with the library)
有四種方法可以編譯并連接AngelScript(原文: There are four ways of compiling and linking with AngelScript in order to use it.),我推薦把它連接成靜態(tài)庫(kù)(原文: I recommend linking with a static library.)。請(qǐng)注意,你只需要少許改動(dòng)代碼這四種方法就可以相互轉(zhuǎn)換了(原文:Note that all four ways are interchangable with only a small change in your code,),即 在包含頭文件之前先定義一個(gè)標(biāo)記,然后可能需要手工加載一個(gè)DLL(原文: i.e a defined flag before including the header file, and possibly a routine for manually loading the dll.)。【接下來(lái)按照下面提供的任何一種方案操作都一樣(原文: The rest of your code should look exactly the same for each of the alternatives.)。】
1.在工程中包含腳本庫(kù)文件(原文: Include library source files in project)
你可以直接在自己的工程中包含AngelScript源文件(原文:You can take the source files for AngelScript and include them directly in your own project.)。
這樣的好處是可以保證腳本庫(kù)和宿主應(yīng)用程序都使用相同的編譯選項(xiàng)(原文:The advantage of this is that you can be sure that the same compiler options are used for the library and the host applications,),例如:CRT(應(yīng)該翻譯成C運(yùn)行時(shí)么?)是多線程還是單線程(原文: e.g. multi-threaded or single-threaded CRT.)。缺點(diǎn)就是你的工程將包含有庫(kù)文件(原文: The disadvantage is that your project will be poluted with the library files.)
這些文件要使用這個(gè)腳本庫(kù),可能需要包含angelscript.h頭再就不需要?jiǎng)e的特殊設(shè)置了(原文: The files that need to use the library should include the angelscript.h header with no need for any special settings.)。
// Include the library interface
#include "angelscript.h"
// ... Start using the library
2. 編譯一個(gè)靜態(tài)庫(kù)并連接到工程中(Compile a static library and link into project)
極力推薦把它編譯成一個(gè)靜態(tài)庫(kù)并連接到自己的工程中(原文: The most recommended way is to compile a static library that your project will link with.)。在編譯靜態(tài)庫(kù)的時(shí)候必須先設(shè)置好編譯選項(xiàng)以免與CRT功能項(xiàng)沖突(功能: When compiling the static library you have to make sure that the correct compiler settings are used so that you don't get conflicts in linkage with the CRT functions.)。例如:如果腳本庫(kù)使用動(dòng)態(tài)鏈接多線程的CRT而你的應(yīng)用程序采用單線程的靜態(tài)鏈接CRT(原文: This happens if you for example compile the library with dynamically linked multi-threaded CRT and your application with statically linked single-threaded CRT.)(在VisualC++中可以在Project -> Settings -> C/C++ -> Category: Code Generation中設(shè)置這些)

接下來(lái),要使用腳本庫(kù)你只需要包含angelscript.h頭文件即可(原文: To use the library you only need to include the angelscript.h header file.)。
// Include the library interface
#include "angelscript.h"
// ... Start using the library
3. 編譯一個(gè)導(dǎo)入庫(kù)的動(dòng)態(tài)鏈接庫(kù)Compile a dynamically loaded library with an import library
使用Microsoft Visual C + +就可以編譯導(dǎo)入庫(kù)動(dòng)態(tài)加載庫(kù)(原文: With Microsoft Visual C++ it is possible to compile a dynamically loaded library with an import library.)。導(dǎo)入庫(kù), 加載需要的DLL并綁定函數(shù)(原文: The import library will then take care of the work needed to load the dll and bind the functions.)。 這種方法可能存在缺點(diǎn),如果腳本庫(kù)加載失敗就沒(méi)有辦法給出友好的錯(cuò)誤提示(原文:A possible disadvantage of this method is that you are not able to give any user-friendly error messages in case loading the library fails.)
在包含angelscript.h頭文件之前定義個(gè)ANGELSCRIPT_DLL_MANUAL_IMPORT宏(原文:To use the library you'll have to define ANGELSCRIPT_DLL_LIBRARY_IMPORT before including the angelscript.h header file.)。
// Include the library interface
#define ANGELSCRIPT_DLL_LIBRARY_IMPORT
#include "angelscript.h"
// ... 開(kāi)始使用腳本庫(kù)
4. 手工載入動(dòng)態(tài)連接庫(kù)(Load the dynamically loaded library manually)
如果你想使用DLL,例如 在應(yīng)用程序之間共享代碼,我建議手工載入這個(gè)庫(kù),這樣基本上所有的加載或綁定錯(cuò)誤都可以得到緩解(原文: If you want to use a dll, e.g. to share code between applications, I recommend loading the library manually as you can treat any failures to load or bind functions graciously.)。
手工載入DLL,你可以在包含angelscript.h頭文件之前定義個(gè)ANGELSCRIPT_DLL_MANUAL_IMPORT宏(原文:To use manually loaded dll, you should define ANGELSCRIPT_DLL_MANUAL_IMPORT before including the header file.)。這樣可以確保在頭文件中不用聲明函數(shù)原型,同樣就可以通過(guò)函數(shù)指針來(lái)使用這些的名字(原文: This will insure that the header file doesn't declare the function prototypes, as you will most likely want to use these names for the function pointers.)。
// Include the library interface
#define ANGELSCRIPT_DLL_MANUAL_IMPORT
#include "angelscript.h"
// 聲明函數(shù)指針
typedef asIScriptEngine * AS_CALL t_asCreateScriptEngine(int);
t_asCreateScriptEngine *asCreateScriptEngine = 0;
// ... 聲明其余函數(shù)
//載入DLL并綁定函數(shù) (error handling left out for clarity)
HMODULE dll = LoadLibrary("angelscript.dll");
asCreateScriptEngine = (t_asCreateScriptEngine*)GetProcAddress(dll, "_asCreateScriptEngine");
…
// 綁定其它函數(shù)
…
// 使用本腳本庫(kù)
…
--------- besterChen
譯于 2010年3月1日星期六