Windows 环境编译 Lua5.3

本文讲述两种 lua 源码编译方式,命令行编译与 VS 编译,以当前最新的 lua-5.3.5 为例,开始编译前请下载并安装 VS2015。

下载 lua 源码

下载地址

命令行编译

  • 解压 lua 源码

  • 在根目录创建批处理文件 msvc_build.bat

  • 打开文件,写入以下命令:

    @echo off
    md bin
    md lib
    md include
    cd src
    
    cl /c /nologo /W3 /O2 /Ob1 /Oi /Gs /MD /D_CRT_SECURE_NO_DEPRECATE l*.c
    ren lua.obj lua.o
    ren luac.obj luac.o
    lib /OUT:lua53.lib *.obj
    copy lua53.lib ..\lib\lua53.lib
    del *.o *.obj *.exp *.lib *.dll *.exe
    
    cl /c /nologo /W3 /O2 /Ob1 /Oi /Gs /MD /D_CRT_SECURE_NO_DEPRECATE /DLUA_BUILD_AS_DLL l*.c
    ren lua.obj lua.o
    ren luac.obj luac.o
    link /DLL /IMPLIB:lua53.lib /OUT:lua53.dll *.obj
    link /OUT:lua53.exe lua.o lua53.lib
    link /OUT:luac53.exe luac.o *.obj
    copy lua53.exp ..\bin\lua53.exp
    copy lua53.lib ..\bin\lua53.lib
    copy lua53.dll ..\bin\lua53.dll
    copy lua53.exe ..\bin\lua53.exe
    copy luac53.exe ..\bin\luac53.exe
    del *.o *.obj *.exp *.lib *.dll *.exe
    
    copy lauxlib.h ..\include\lauxlib.h
    copy lua.h ..\include\lua.h
    copy lua.hpp ..\include\lua.hpp
    copy luaconf.h ..\include\luaconf.h
    copy lualib.h ..\include\lualib.h
    cd ..\
  • 打开:开始 -> Visual Studio 2015 -> VS2015 开发人员命令提示,cd 到 lua 源码根目录

  • 运行 msvc_build.bat,等待编译结果,结果如下:

使用 VS2015 编译

  • 创建空项目 lua53,将 lua.c 添加进该项目。

  • 解决方案'lua53' 下添加新的 Win 32 项目 luadll:在引导弹窗中选择下一步,应用程序类型选择 DLL,附加选项选择空项目,点击完成,将 lua.cluac.c 以外的文件全部添加进该项目。

  • lua53 项目添加引用,方法如图所示:

  • lua53 项目附加添加包含目录为 luadll 头文件所在目录,方法如图所示:

  • luadll 输出文件名改为 lua53,方法如图所示:

  • luadll 添加预处理器定义 LUA_BUILD_AS_DLL ,方法如图所示:

  • 在解决方案'lua53'下添加新的空项目 luac53,将 lua.c 以外的文件全部添加进该项目。

  • 分别选择 lua53 luac53 ,右键生成项目。

  • 生成结果如下:

遇到的问题及解决方案

  1. luadll 编译时未生成 .lib 文件,解决方法:添加预处理器定义 LUA_BUILD_AS_DLL,原因见 luaconf.h:

    /*
    @@ LUA_API is a mark for all core API functions.
    @@ LUALIB_API is a mark for all auxiliary library functions.
    @@ LUAMOD_API is a mark for all standard library opening functions.
    ** CHANGE them if you need to define those functions in some special way.
    ** For instance, if you want to create one Windows DLL with the core and
    ** the libraries, you may want to use the following definition (define
    ** LUA_BUILD_AS_DLL to get it).
    */
    #if defined(LUA_BUILD_AS_DLL)    /* { */
    
    #if defined(LUA_CORE) || defined(LUA_LIB)    /* { */
    #define LUA_API __declspec(dllexport)
    #else                        /* }{ */
    #define LUA_API __declspec(dllimport)
    #endif                       /* } */
    
    #else                /* }{ */
    
    #define LUA_API      extern
    
    #endif               /* } */
  2. luac53 不能以动态链接库的方式进行编译,这是因为有一些核心函数并未进行导出。

本文项目工程链接

lua53 VS2015项目

请将项目中的绝对路径换成你自己的路径

参考链接

为Lua5.3编写C模块简单示例

猜你喜欢

转载自www.cnblogs.com/zhouzl/p/9545514.html