[翻译]AngelScript 实用手册之 编译AngelScript脚本库(Compile the library)

       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 实用手册之 编译AngelScript脚本库(Compile the library)_第1张图片

       接下来,要使用脚本库你只需要包含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

                                                                                                         译于 201031日星期六

你可能感兴趣的:([翻译]AngelScript 实用手册之 编译AngelScript脚本库(Compile the library))