一個lua問題解決過程

lt發表於2016-10-11

我在執行一個lua檔案的時候,遇到以下報錯資訊


D:\polycube-solver-master>\a\timer \lua514\lua5.1 polycube-solver.lua problems/soma-cube.txt >soma-res-jit2.txt
\lua514\lua5.1: .\bitmatrix.lua:4: module 'bit' not found:
        no field package.preload['bit']
        no file '.\bit.lua'
        no file 'D:\lua514\lua\bit.lua'
        no file 'D:\lua514\lua\bit\init.lua'
        no file 'D:\lua514\bit.lua'
        no file 'D:\lua514\bit\init.lua'
        no file '.\bit.dll'
        no file '.\bit51.dll'
        no file 'D:\lua514\bit.dll'
        no file 'D:\lua514\bit51.dll'
        no file 'D:\lua514\clibs\bit.dll'
        no file 'D:\lua514\clibs\bit51.dll'
        no file 'D:\lua514\loadall.dll'
        no file 'D:\lua514\clibs\loadall.dll'
stack traceback:
        [C]: in function 'require'
        .\bitmatrix.lua:4: in main chunk
        [C]: in function 'require'
        polycube-solver.lua:4: in main chunk
        [C]: ?

而相同的檔案,使用作者提供的包中的luajit和自己編譯的LuaJIT-2.1.0-beta2可執行檔案是沒有問題的。


D:\polycube-solver-master>\a\timer lua-win\luajit polycube-solver.lua problems/soma-cube.txt >soma-res-jit2.txt

D:\polycube-solver-master>\a\timer D:\LuaJIT-2.1.0-beta2\src\luajit polycube-solver.lua problems/soma-cube.txt >soma-res-jit.txt

查閱網上資訊得知,lua自己沒有內建位運算,需要使用擴充套件庫,才能在lua原始檔中require "bit"。從http://bitop.luajit.org/下載的LuaBitOp-1.0.2.zip,但這是個原始碼,不能直接執行。 按照裡面文件的提示,把這個壓縮包中的檔案釋放到lua原始碼所在目錄,然後執行msvcbuild.bat

D:\lua5.1\src>msvcbuild

D:\lua5.1\src>cl /nologo /MD /O2 /W3 /c -I .. bit.c
bit.c

D:\lua5.1\src>link /nologo /DLL /export:luaopen_bit /out:bit.dll bit.obj ..\lua51.lib
LINK : fatal error LNK1181: cannot open input file '..\lua51.lib'

D:\lua5.1\src>if exist bit.dll.manifest mt /nologo -manifest bit.dll.manifest -outputresource:bit.dll;2

D:\lua5.1\src>del *.obj *.exp *.manifest

提示缺少lib檔案,把下載的lua5.1.lib檔案改名為lua51.lib後放到src的上級目錄,重新執行msvcbuild.bat,編譯完成。

D:\lua5.1\src>msvcbuild

D:\lua5.1\src>cl /nologo /MD /O2 /W3 /c -I .. bit.c
bit.c

D:\lua5.1\src>link /nologo /DLL /export:luaopen_bit /out:bit.dll bit.obj ..\lua51.lib
   Creating library bit.lib and object bit.exp

D:\lua5.1\src>if exist bit.dll.manifest mt /nologo -manifest bit.dll.manifest -outputresource:bit.dll;2

D:\lua5.1\src>del *.obj *.exp *.manifest

從https://sourceforge.net/projects/luabinaries/下載相應的原始碼、可執行檔案和庫檔案。 我用的檔案分別是:

lua-5.1.4_Win32_dll11_lib.zip(這是編譯dll檔案用的)
lua-5.1.4_Win32_vc11_lib.zip(這是編譯exe檔案用的,不能用於本文)

lua5_1_4_Win32_bin.zip
lua5_1_4_Sources.zip

最後,把編譯出來的bit.dll放置到lua5.1.exe檔案所在目錄。上述lua檔案就能順利執行了。

D:\polycube-solver-master>\lua514\lua5.1 polycube-solver.lua problems\toy3.txt
Polycube Solver 1.0
Box dimensions: 3x3x1
Pieces: 1_ 3L V_
Locked: V_ (1)
Problem matrix: 26x12
。。。
D:\polycube-solver-master>\a\timer \lua514\lua5.1 polycube-solver.lua problems/soma-cube.txt >soma-res-nojit.txt

事後發現,編譯bit.dll不需要完整lua原始碼,只要有lua-5.1.4_Win32_dll11_lib.zip中的檔案就夠了,如下所示

D:\lua_dll>set include=%include%;D:\lua_dll\include

D:\lua_dll>msvcbuild

D:\lua_dll>cl /nologo /MD /O2 /W3 /c -I .. bit.c
bit.c

D:\lua_dll>link /nologo /DLL /export:luaopen_bit /out:bit.dll bit.obj ..\lua51.lib
   Creating library bit.lib and object bit.exp

D:\lua_dll>if exist bit.dll.manifest mt /nologo -manifest bit.dll.manifest -outputresource:bit.dll;2

D:\lua_dll>del *.obj *.exp *.manifest

自己太不仔細,原來D:\polycube-solver-master\lua-win目錄下就有一個bit.dll,所以,你懂的。 而LuaJIT的原始碼中已經包含了lib_bit.c。

相關文章