在SDK/angelscript/projects目錄中,您可以找到許多流行編譯器的相關項目文件(原文: In the sdk/angelscript/projects directory you'll find project files for many of the popular compilers)。不過,那些工程文件不一定是最新的腳本庫工程。(原文: these project files are not always up to date with the latest version of the library.)如果你有任何一個編譯或鏈接出錯,請弄清楚工程文件包含的所有文件所在的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),并將工程按照本文件進行設置(原文: and that the project settings are set according to this article.)
,
如果你找不到符合你編譯器的工程文件,你可以通過添加sdk/angelscript/source目錄下的全部文件到你的工程,并且對項目作適當的配置(原文: 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.)。
如果你有一個從沒使用過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. 編譯時的選項設置(Set compile time options)
創建as_config.h頭文件是為了使本代碼盡可能的包含不同的編譯環境(原文: The code tries to contain compiler differences in as few places as possible. The header as_config.h was created for that purpose.)。為了支持不同的編譯器,你可能會發現一些 #defines 宏定義(There you will find some #defines that allow different compilers to work.)。這個文件一般情況下不需要修改,但是如果你用一個過舊的編譯器編譯出錯的話,可能就需要看一下這個文件了(原文: 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.)。
【當在編譯這個庫時有一些 #defines 宏定義需要改寫,比如寫你應該定義一個ANGELSCRIPT_EXPORT宏以便導出庫函數(原文: 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.)。】當然,如果在你的工程中包含了庫源代碼目錄的話就不用定義這個標記了(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.)。盡管不能保證支持向后兼容,但是應盡可能的移除不支持的功能(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. 關聯腳本庫(Linking with the library)
有四種方法可以編譯并連接AngelScript(原文: There are four ways of compiling and linking with AngelScript in order to use it.),我推薦把它連接成靜態庫(原文: I recommend linking with a static library.)。請注意,你只需要少許改動代碼這四種方法就可以相互轉換了(原文:Note that all four ways are interchangable with only a small change in your code,),即 在包含頭文件之前先定義一個標記,然后可能需要手工加載一個DLL(原文: i.e a defined flag before including the header file, and possibly a routine for manually loading the dll.)。【接下來按照下面提供的任何一種方案操作都一樣(原文: The rest of your code should look exactly the same for each of the alternatives.)。】
1.在工程中包含腳本庫文件(原文: Include library source files in project)
你可以直接在自己的工程中包含AngelScript源文件(原文:You can take the source files for AngelScript and include them directly in your own project.)。
這樣的好處是可以保證腳本庫和宿主應用程序都使用相同的編譯選項(原文: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(應該翻譯成C運行時么?)是多線程還是單線程(原文: e.g. multi-threaded or single-threaded CRT.)。缺點就是你的工程將包含有庫文件(原文: The disadvantage is that your project will be poluted with the library files.)
這些文件要使用這個腳本庫,可能需要包含angelscript.h頭再就不需要別的特殊設置了(原文: 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. 編譯一個靜態庫并連接到工程中(Compile a static library and link into project)
極力推薦把它編譯成一個靜態庫并連接到自己的工程中(原文: The most recommended way is to compile a static library that your project will link with.)。在編譯靜態庫的時候必須先設置好編譯選項以免與CRT功能項沖突(功能: 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.)。例如:如果腳本庫使用動態鏈接多線程的CRT而你的應用程序采用單線程的靜態鏈接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中設置這些)

接下來,要使用腳本庫你只需要包含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. 編譯一個導入庫的動態鏈接庫Compile a dynamically loaded library with an import library
使用Microsoft Visual C + +就可以編譯導入庫動態加載庫(原文: With Microsoft Visual C++ it is possible to compile a dynamically loaded library with an import library.)。導入庫, 加載需要的DLL并綁定函數(原文: The import library will then take care of the work needed to load the dll and bind the functions.)。 這種方法可能存在缺點,如果腳本庫加載失敗就沒有辦法給出友好的錯誤提示(原文: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頭文件之前定義個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"
// ... 開始使用腳本庫
4. 手工載入動態連接庫(Load the dynamically loaded library manually)
如果你想使用DLL,例如 在應用程序之間共享代碼,我建議手工載入這個庫,這樣基本上所有的加載或綁定錯誤都可以得到緩解(原文: 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頭文件之前定義個ANGELSCRIPT_DLL_MANUAL_IMPORT宏(原文:To use manually loaded dll, you should define ANGELSCRIPT_DLL_MANUAL_IMPORT before including the header file.)。這樣可以確保在頭文件中不用聲明函數原型,同樣就可以通過函數指針來使用這些的名字(原文: 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"
// 聲明函數指針
typedef asIScriptEngine * AS_CALL t_asCreateScriptEngine(int);
t_asCreateScriptEngine *asCreateScriptEngine = 0;
// ... 聲明其余函數
//載入DLL并綁定函數 (error handling left out for clarity)
HMODULE dll = LoadLibrary("angelscript.dll");
asCreateScriptEngine = (t_asCreateScriptEngine*)GetProcAddress(dll, "_asCreateScriptEngine");
…
// 綁定其它函數
…
// 使用本腳本庫
…
--------- besterChen
譯于 2010年3月1日星期六