在windows下执行lua程序调用动态库的时候,发现:
D:\My Documents\codeprojec\test_so\bin\Debug>lua.exe look.lua
lua.exe: error loading module ‘stacklook’ from file ‘./stacklook.dll': dynamic libraries not enabled; check your Lua installation
追述原因就是没有编译动态库加载,这样我们修改lua源码文件下的 src/Makefile文件找到ansi 修改成这样:ansi:$(MAKE) all MYCFLAGS=-DLUA_ANSI MYCFLAGS+=-DLUA_USE_DLOPEN MYLIBS=”-ldl”这样可以用mingw编译了。再按编译过成中mingw需要下载个文件http://code.google.com/p/dlfcn-win32/ 这个下载dlfcn-win32-static-r19.tar.bz2 解压到mingw对应的include 和lib路径下。
D:\lua-5.1>.\mingw32-make.exe ansi
cd src && D:/lua-5.1/mingw32-make ansi
mingw32-make[1]: Entering directory `d:/lua-5.1/src’
d:/lua-5.1/mingw32-make all MYCFLAGS=-DLUA_ANSI MYCFLAGS+=-DLUA_USE_DLOPEN MYLIBS=”-ldl”
mingw32-make[2]: Entering directory `d:/lua-5.1/src’
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lapi.o lapi.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lcode.o lcode.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ldebug.o ldebug.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ldo.o ldo.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ldump.o ldump.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lfunc.o lfunc.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lgc.o lgc.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o llex.o llex.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lmem.o lmem.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lobject.o lobject.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lopcodes.o lopcodes.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lparser.o lparser.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lstate.o lstate.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lstring.o lstring.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ltable.o ltable.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ltm.o ltm.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lundump.o lundump.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lvm.o lvm.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lzio.o lzio.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lauxlib.o lauxlib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lbaselib.o lbaselib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ldblib.o ldblib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o liolib.o liolib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lmathlib.o lmathlib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o loslib.o loslib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o ltablib.o ltablib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lstrlib.o lstrlib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o loadlib.o loadlib.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.
o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o l
strlib.o loadlib.o linit.o
ranlib liblua.a
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o lua.o lua.c
gcc -o lua lua.o liblua.a -lm -ldl
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o luac.o luac.c
gcc -O2 -Wall -DLUA_ANSI -DLUA_USE_DLOPEN -c -o print.o print.c
gcc -o luac luac.o print.o liblua.a -lm -ldl
mingw32-make[2]: Leaving directory `d:/lua-5.1/src’
mingw32-make[1]: Leaving directory `d:/lua-5.1/src’
编译成功。OK。继续加载,OK,成功通过,这是我的程序运行结果如下:
D:\My Documents\codeprojec\test_so\bin\Debug>lua.exe look.lua
Initial stack [ 1 2 3 4 5 6 7 ]
lua_settop(L, 3) [ 1 2 3 ]
lua_settop(L, 5) [ 1 2 3 - - ]
lua_pushinteger(L, 5) [ 1 2 3 - - 5 ]
lua_pushinteger(L, 4) [ 1 2 3 - - 5 4 ]
lua_replace(L, -4) [ 1 2 3 4 - 5 ]
lua_replace(L, 5) [ 1 2 3 4 5 ]
lua_remove(L, 3) [ 1 2 4 5 ]
lua_pushinteger(L, 3) [ 1 2 4 5 3 ]
lua_insert(L, -3) [ 1 2 3 4 5 ]
lua_pushvalue(L, 2) [ 1 2 3 4 5 2 ]
lua_pop(L, 1) [ 1 2 3 4 5 ]
stack.look 3 4 5