Luajit 2.0.4 绑定 protoc-gen-lua (Windows 平台)

转载自:https://blog.csdn.net/huutu/article/details/49696795

Luajit 2.0.4 绑定 protoc-gen-lua (Windows 平台)

上一篇  protoc-gen-lua 编译、安装、使用教程   介绍了 protoc-gen-lua 的编译使用介绍,但是最后的例子,我是把 protoc-gen-lua 的 pb.c 源代码添加到工程中去进行编译的。那如果我们写了 lua 代码想直接用 lua 命令测试呢?这时候总不能每次都拿 vs 的工程来编译运行,好在我们有 Luajit 2.0.4 的源代码 和 protoc-gen-lua 中 pb.c 的源代码,代码在手天下我有,我来把这两个整合到一起,即把 protoc-gen-lua 的 pb.c 编译到 Luajit 2.0.4 中。

 

首先下载安装 Luajit 2.0.4 ,然后解压、编译。具体过程请看上一篇 。

 

然后下载安装 protobuf-2.4.1,然后解压、编译。具体过程请看上一篇

 

然后下载安装 protoc-gen-lua ,然后解压、编译、具体过程请看上一篇

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

说这么多就是说 看完上一篇 再来看这一篇……

Luajit 2.0.4 的编译是在 LuaJIT-2.0.4\src 中,在 Visual Studio 控制台中 执行 msvcbuild.bat 进行编译的,编译会生成 lua51.dll 、lua51.lib、luajit.exe  。

首先,拷贝 protoc-gen-lua-master\protobuf\pb.c  到 LuaJIT-2.0.4\src 目录,然后用文本编辑器打开 msvcbuild.bat ,添加 pb.c 到链接文件列表中,修改如下

 

  1.  
    @rem Script to build LuaJIT with MSVC.
  2.  
    @rem Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  3.  
    @rem
  4.  
    @rem Either open a "Visual Studio .NET Command Prompt"
  5.  
    @rem (Note that the Express Edition does not contain an x64 compiler)
  6.  
    @rem -or-
  7.  
    @rem Open a "Windows SDK Command Shell" and set the compiler environment:
  8.  
    @rem setenv /release /x86
  9.  
    @rem -or-
  10.  
    @rem setenv /release /x64
  11.  
    @rem
  12.  
    @rem Then cd to this directory and run this script.
  13.  
     
  14.  
    @if not defined INCLUDE goto :FAIL
  15.  
     
  16.  
    @setlocal
  17.  
    @set LJCOMPILE=cl /nologo /c /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE
  18.  
    @set LJLINK=link /nologo
  19.  
    @set LJMT=mt /nologo
  20.  
    @set LJLIB=lib /nologo /nodefaultlib
  21.  
    @set DASMDIR=..\dynasm
  22.  
    @set DASM=%DASMDIR%\dynasm.lua
  23.  
    @set LJDLLNAME=lua51.dll
  24.  
    @set LJLIBNAME=lua51.lib
  25.  
    @set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c
  26.  
     
  27.  
    %LJCOMPILE% host\minilua.c
  28.  
    @if errorlevel 1 goto :BAD
  29.  
    %LJLINK% /out:minilua.exe minilua.obj
  30.  
    @if errorlevel 1 goto :BAD
  31.  
    if exist minilua.exe.manifest^
  32.  
    %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe
  33.  
     
  34.  
    @set DASMFLAGS=-D WIN -D JIT -D FFI -D P64
  35.  
    @set LJARCH=x64
  36.  
    @minilua
  37.  
    @if errorlevel 8 goto :X64
  38.  
    @set DASMFLAGS=-D WIN -D JIT -D FFI
  39.  
    @set LJARCH=x86
  40.  
    :X64
  41.  
    minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x86.dasc
  42.  
    @if errorlevel 1 goto :BAD
  43.  
     
  44.  
    %LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c
  45.  
    @if errorlevel 1 goto :BAD
  46.  
    %LJLINK% /out:buildvm.exe buildvm*.obj
  47.  
    @if errorlevel 1 goto :BAD
  48.  
    if exist buildvm.exe.manifest^
  49.  
    %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe
  50.  
     
  51.  
    buildvm -m peobj -o lj_vm.obj
  52.  
    @if errorlevel 1 goto :BAD
  53.  
    buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%
  54.  
    @if errorlevel 1 goto :BAD
  55.  
    buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%
  56.  
    @if errorlevel 1 goto :BAD
  57.  
    buildvm -m libdef -o lj_libdef.h %ALL_LIB%
  58.  
    @if errorlevel 1 goto :BAD
  59.  
    buildvm -m recdef -o lj_recdef.h %ALL_LIB%
  60.  
    @if errorlevel 1 goto :BAD
  61.  
    buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%
  62.  
    @if errorlevel 1 goto :BAD
  63.  
    buildvm -m folddef -o lj_folddef.h lj_opt_fold.c
  64.  
    @if errorlevel 1 goto :BAD
  65.  
     
  66.  
    @if "%1" neq "debug" goto :NODEBUG
  67.  
    @shift
  68.  
    @set LJCOMPILE=%LJCOMPILE% /Zi
  69.  
    @set LJLINK=%LJLINK% /debug
  70.  
    :NODEBUG
  71.  
    @if "%1"=="amalg" goto :AMALGDLL
  72.  
    @if "%1"=="static" goto :STATIC
  73.  
    %LJCOMPILE% /MD /DLUA_BUILD_AS_DLL lj_*.c lib_*.c pb.c
  74.  
    @if errorlevel 1 goto :BAD
  75.  
    %LJLINK% /DLL /out:%LJDLLNAME% lj_*.obj lib_*.obj pb.obj
  76.  
    @if errorlevel 1 goto :BAD
  77.  
    @goto :MTDLL
  78.  
    :STATIC
  79.  
    %LJCOMPILE% lj_*.c lib_*.c pb.c
  80.  
    @if errorlevel 1 goto :BAD
  81.  
    %LJLIB% /OUT:%LJLIBNAME% lj_*.obj lib_*.obj pb.obj
  82.  
    @if errorlevel 1 goto :BAD
  83.  
    @goto :MTDLL
  84.  
    :AMALGDLL
  85.  
    %LJCOMPILE% /MD /DLUA_BUILD_AS_DLL ljamalg.c
  86.  
    @if errorlevel 1 goto :BAD
  87.  
    %LJLINK% /DLL /out:%LJDLLNAME% ljamalg.obj lj_vm.obj
  88.  
    @if errorlevel 1 goto :BAD
  89.  
    :MTDLL
  90.  
    if exist %LJDLLNAME%.manifest^
  91.  
    %LJMT% -manifest %LJDLLNAME%.manifest -outputresource:%LJDLLNAME%;2
  92.  
     
  93.  
    %LJCOMPILE% luajit.c pb.c
  94.  
    @if errorlevel 1 goto :BAD
  95.  
    %LJLINK% /out:luajit.exe luajit.obj pb.obj %LJLIBNAME%
  96.  
    @if errorlevel 1 goto :BAD
  97.  
    if exist luajit.exe.manifest^
  98.  
    %LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe
  99.  
     
  100.  
    @del *.obj *.manifest minilua.exe buildvm.exe
  101.  
    @echo.
  102.  
    @echo === Successfully built LuaJIT for Windows/%LJARCH% ===
  103.  
     
  104.  
    @goto :END
  105.  
    :BAD
  106.  
    @echo.
  107.  
    @echo *******************************************************
  108.  
    @echo *** Build FAILED -- Please check the error messages ***
  109.  
    @echo *******************************************************
  110.  
    @goto :END
  111.  
    :FAIL
  112.  
    @echo You must open a "Visual Studio .NET Command Prompt" to run this script
  113.  
    :END

 

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn


具体添加的内容 如图

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

然后 执行  msvcbuild.bat 编译,肯定会出错的……,如下图

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

提示打不开 lua.h  ,原因是 pb.c 中是用 #include<lua.h> 这种模式导入头文件

修改成 #include "lua.h" 即可

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

修改后再次编译,还是会报错,不过这个错 上一篇 已经遇到过了。

是因为宏定义的问题,引入了不该引入的头文件。

用宏定义隔开不执行。

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

再次编译,编译成功。

拿上一篇的测试工程,将 pb.c 从项目中移除,然后编译工程,就会提示错误

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

原因是:虽然 pb.c 被添加到了 msvcbuild.bat   中,但是 luaopen_pb 这个函数并没有导出,所以我们再对 pb.c 进行修改。让这个函数导出

首先在开始 添加宏定义 

 

#define LUA_LIB

然后给函数 

 

 

int luaopen_pb (lua_State *L)

添加导出属性,修改为

 

 

LUA_API int luaopen_pb (lua_State *L)

再次执行 msvcbuild.bat 编译 LuaJIT-2.0.4 ,编译成功。

 

再次编译测试工程,编译成功。

把生成的 lua51.dll 拷贝到项目文件夹,运行,成功。

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

但这并不是我们的目的,继续。

 

用编译  LuaJIT-2.0.4  生成的 luajit.exe 去执行测试工程中的 main.lua ,发现并不能运行

 

原因是虽然 pb.c 中的 int luaopen_pb (lua_State *L) 被修改成了 LUA_API int luaopen_pb (lua_State *L) ,编译的时候导出了可以调用,但是在 luajit.exe 中,我们并没有调用

LUA_API int luaopen_pb (lua_State *L) 这个函数!!所以没有 load protobuf 的库。

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

打开 LuaJIT-2.0.4\src\luajit.c  ,在 static int pmain(lua_State *L)  函数中添加 LUA_API int luaopen_pb (lua_State *L) 函数的调用。

如下图红框标识新添加的代码

转自 http://blog.csdn.net/huutu http://www.thisisgame.com.cn

再次执行 msvcbuild.bat  编译 LuaJIT-2.0.4 。

再次用 luajit.exe 去运行 main.lua  脚本,终于成功运行!

posted on 2018-08-08 10:00  魔天天  阅读(625)  评论(0)    收藏  举报