Lua移植到arm上 并实现在arm上 可以让lua脚本调c语言,C语言调用lua脚本
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?<math.h>
#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?<stdio.h>???
#include?<lua.h>??
#include?<lualib.h>??
#include?<lauxlib.h>??
??
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
? ?
lua編譯問題總結
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
總結
以上是生活随笔為你收集整理的Lua移植到arm上 并实现在arm上 可以让lua脚本调c语言,C语言调用lua脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Basic脚本解释器移植到STM32
- 下一篇: eLua学习第一课:和Lua的第一次亲密