Lua移植到arm上 并实现在arm上 可以让lua脚本调c语言,C语言调用lua脚本
首先参考http://wiki.chumby.com/index.php?title=Lua&printable=yes上的做法,修改lua-5.1.4.tar.gz. 上的Makefile,编译过后会在/src目录下生成可以在arm-linux上可以运行的lua解析器和luac编译器。我们在arm-linux下运行lua脚本,只需要用到lua解析器就ok了,所以,我们把lua cp到我们开发板linux系统上的 /bin目录下就ok了。这样,在开发板上的linux系统就可以自动地找到lua并使用它了。
Lua
From ChumbyWiki
Jump to: navigation, search
Lua is a lightweight scripting language - see the Lua website
Building lua
· download and unpack the lua source distribution lua-5.1.4.tar.gz.
· edit src/Makefile
o line 10
CC=arm-linux-gcc
o line 12
AR=arm-linux-ar
o line 13
RANLIB=arm-linux-ranlib
o line 15
LIBS= -lm $(MYLIBS) -static
o line 99, remove -lreadline -lhistory -lncurses //这里可能行数有点偏差
把这行的内容$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl -lreadline -lhistory -lncurses"
改成 $(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS="-Wl,-E -ldl "
· edit src/luaconf.h, comment out line 39 (disable LUA_USE_READLINE)
· do:
make linux
arm-linux-strip src/lua
接下来,我们看看如何实现在arm linux上实现让lua脚本调c语言:
1.写一个jiaohu.c文件:
#include
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
static int my_foo(lua_State* L)
{
int n = lua_gettop(L);
int isnum = lua_isnumber(L, 1);
int m = lua_tonumber(L, 1);
printf("%d %d %d\n", n, isnum, m);
lua_pushnumber(L,m);
lua_pushstring(L,"foo ret string\n");
return 2;
}
LUALIB_API int my_init(lua_State* L)
{
lua_register(L, "foo", my_foo);
return 0;
}
用 arm-linux-gcc -I/root/lua-5.1.4/src/ -L/root/lua-5.1.4/src/ -shared -fPIC jiaohu.c -o jiaohu.so编译,这样就把可以加到lua上的动态库编译好了。
再写一个lua的调用脚本 test.lua:
package.loadlib("./jiaohu.so", "my_init")()
a ,b=foo(22,33)
接下来就是把jiahu.so 和 test.lua放到arm linux的某个测试用的临时目录下就ok了
然后执行 一下 >lua test.lua
显示 >1 1 22
成功实现在arm linux上实现让lua脚本调c语言了
现在该来看看如何实现在arm linux下如何实现 C调用lua脚本了
先建立一个 test.c文件:
#include
#include
#include
#include
lua_State* L;
int main ( int argc, char *argv[] ){
L = luaL_newstate();
luaL_openlibs(L);
// luaL_dofile(L, "test.lua");
luaL_loadfile(L, "./test.lua");
int iError = lua_pcall(L, 0, 0, 0);
if (iError)
{
printf("error %d \n",iError);
lua_close(L);
return 1;
}
int a = 11 ;
int b = 12 ;
lua_getglobal(L,"sum");
lua_pushinteger(L,a) ;
lua_pushinteger(L,b) ;
int ret = lua_pcall(L,2,1,0) ;
if ( ret != 0 )
{
printf("error\n");
return 1;
}
printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(L,-1)) ;
lua_pop(L,1);
/* 清除Lua */
lua_close(L);
return 0;
}
再建立一个test.lua文件
width=1 ;
height=2 ;
function sum(a,b)
return a+b ;
End
用一下命令来编译test.c文件
#arm-linux-gcc -I/root/lua_tool/lua-5.1.4-armlinux/src/ -L/root/lua_tool/lua-5.1.4-armlinux/src/ -lm -DLUA_USE_READLINE test.c /root/lua_tool/lua-5.1.4-armlinux/src/liblua.a -o test2 -ldl
把生成的test2 和test.lua文件拷到arm linux上运行:
#Lua test2
就会显示#sum:11 + 12 = 23
参考文章:
http://hi.baidu.com/hqwfreefly/blog/item/5724afef3b6018dab31cb1d1.html
http://hi.baidu.com/mikenoodle/blog/item/5fd129dd1069a9de8c102948.html
http://wenku.baidu.com/view/0d557b0416fc700abb68fc73.html
http://www.extgui.com/?p=157
gcc -lm -g -o test test.c /usr/local/lib/liblua.a -ldl
如果少-ldl,那么编译就会报:
gcc -lm -g -o test test.c /usr/local/lib/liblua.a
/usr/local/lib/liblua.a(loadlib.o): In function `gctm':
loadlib.c:(.text+0x35): undefined reference to `dlclose'
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0xc0): undefined reference to `dlopen'
loadlib.c:(.text+0xfc): undefined reference to `dlsym'
loadlib.c:(.text+0x198): undefined reference to `dlerror'
loadlib.c:(.text+0x1bb): undefined reference to `dlerror'
如果少liblua.a ,就会报如下问题:
[wangbin@tuan lua]$ gcc -lm -g -o test test.c -ldl
/tmp/ccCT0d24.o: In function `main':
/home/wangbin/work/tmp/lua/test.c:26: undefined reference to `luaL_newstate'
/home/wangbin/work/tmp/lua/test.c:27: undefined reference to `luaL_openlibs'
/home/wangbin/work/tmp/lua/test.c:29: undefined reference to `luaL_loadbufferx'
/home/wangbin/work/tmp/lua/test.c:29: undefined reference to `lua_pcallk'
/home/wangbin/work/tmp/lua/test.c:32: undefined reference to `lua_tolstring'
/home/wangbin/work/tmp/lua/test.c:33: undefined reference to `lua_settop'
/home/wangbin/work/tmp/lua/test.c:36: undefined reference to `lua_close'
collect2: ld returned 1 exit status
如果少-lm,那么编译结果如下:
[wangbin@tuan lua]$ gcc -g -o test test.c /usr/local/lib/liblua.a -ldl
/usr/local/lib/liblua.a(lobject.o): In function `luaO_arith':
lobject.c:(.text+0xdf): undefined reference to `pow'
/usr/local/lib/liblua.a(lvm.o): In function `luaV_execute':
lvm.c:(.text+0x159a): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sin':
lmathlib.c:(.text+0x3e): undefined reference to `sin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sinh':
lmathlib.c:(.text+0x6e): undefined reference to `sinh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cos':
lmathlib.c:(.text+0x9e): undefined reference to `cos'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cosh':
lmathlib.c:(.text+0xce): undefined reference to `cosh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tan':
lmathlib.c:(.text+0xfe): undefined reference to `tan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tanh':
lmathlib.c:(.text+0x12e): undefined reference to `tanh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_asin':
lmathlib.c:(.text+0x15e): undefined reference to `asin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
lmathlib.c:(.text+0x18e): undefined reference to `acos'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan':
lmathlib.c:(.text+0x1be): undefined reference to `atan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan2':
lmathlib.c:(.text+0x1fb): undefined reference to `atan2'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_fmod':
lmathlib.c:(.text+0x2db): undefined reference to `fmod'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sqrt':
lmathlib.c:(.text+0x391): undefined reference to `sqrt'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_pow':
lmathlib.c:(.text+0x3d3): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log':
lmathlib.c:(.text+0x450): undefined reference to `log'
lmathlib.c:(.text+0x460): undefined reference to `log'
lmathlib.c:(.text+0x486): undefined reference to `log10'
lmathlib.c:(.text+0x4aa): undefined reference to `log'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log10':
lmathlib.c:(.text+0x4de): undefined reference to `log10'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_exp':
lmathlib.c:(.text+0x50e): undefined reference to `exp'
collect2: ld returned 1 exit status