反反復復學lua N多次了,這次終於在C中調用lua成功了!一大進步啊!
記錄下過程:
1、找到代碼如下:
//
add.c
#include <stdio.h>
#include " lua.h "
#include " lualib.h "
#include " lauxlib.h "
/* the lua interpreter */
lua_State* L;
int
luaadd( int x, int y)
{
int sum;
/* the function name */
lua_getglobal(L, " add ");
/* the first argument */
lua_pushnumber(L, x);
/* the second argument */
lua_pushnumber(L, y);
/* call the function with 2 arguments, return 1 result. */
lua_call(L, 2, 1);
/* get the result. */
sum = ( int)lua_tonumber(L, - 1);
/* cleanup the return */
lua_pop(L, 1);
return sum;
}
int
main( int argc, char *argv[])
{
int sum;
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the script */
luaL_dofile(L, " add.lua ");
/* call the add function */
sum = luaadd( 10, 15);
/* print the result */
printf( " The sum is %d \n ",sum);
/* cleanup Lua */
lua_close(L);
return 0;
#include <stdio.h>
#include " lua.h "
#include " lualib.h "
#include " lauxlib.h "
/* the lua interpreter */
lua_State* L;
int
luaadd( int x, int y)
{
int sum;
/* the function name */
lua_getglobal(L, " add ");
/* the first argument */
lua_pushnumber(L, x);
/* the second argument */
lua_pushnumber(L, y);
/* call the function with 2 arguments, return 1 result. */
lua_call(L, 2, 1);
/* get the result. */
sum = ( int)lua_tonumber(L, - 1);
/* cleanup the return */
lua_pop(L, 1);
return sum;
}
int
main( int argc, char *argv[])
{
int sum;
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaL_openlibs(L);
/* load the script */
luaL_dofile(L, " add.lua ");
/* call the add function */
sum = luaadd( 10, 15);
/* print the result */
printf( " The sum is %d \n ",sum);
/* cleanup Lua */
lua_close(L);
return 0;
}
然后,lua.add的代碼如下
--
add two numbers
function add(x,y)
return x + y
end
function add(x,y)
return x + y
end
然后,嘗試編譯:gcc -o add add.c
提示: fatal error: lua.h: No such file or directory
然后,locate lua.h,無果。。。
原來在ubuntu下lua的安裝包,binary和dev是分開裝的,找了一下,需要安裝另外一個包liblua5.1-dev
唔,裝好了還是需要用-I指定,所以先找下lua.h被放哪里的。
sudo updatedb
locate lua.h
顯示的結果:
/home/zxluo/Documents/backup/Work/luascript/AndroLua/jni/lua/lua.h
/usr/include/lua5.1/lua.h
/usr/include/lua5.1/lua.hpp
/usr/src/linux-headers-3.2.0-23-generic/include/config/scsi/dh/alua.h
/usr/src/linux-headers-3.2.0-24-generic/include/config/scsi/dh/alua.h
所以,添加文件再次編譯:gcc -o add add.c -I /usr/include/lua5.1/
出現提示:
/tmp/ccPtEQyI.o: In function `luaadd':
main.c:(.text+0x23): undefined reference to `lua_getfield'
main.c:(.text+0x37): undefined reference to `lua_pushnumber'
main.c:(.text+0x4b): undefined reference to `lua_pushnumber'
main.c:(.text+0x64): undefined reference to `lua_call'
main.c:(.text+0x78): undefined reference to `lua_tonumber'
main.c:(.text+0x93): undefined reference to `lua_settop'
/tmp/ccPtEQyI.o: In function `main':
main.c:(.text+0xac): undefined reference to `luaL_newstate'
main.c:(.text+0xc2): undefined reference to `luaL_openlibs'
main.c:(.text+0xd6): undefined reference to `luaL_loadfile'
main.c:(.text+0xf8): undefined reference to `lua_pcall'
main.c:(.text+0x133): undefined reference to `lua_close'
collect2: ld returned 1 exit status
那個啥,是沒有把庫link過來,gcc不知道要把這些函數ld到哪里去,繼續找liblua:
locate liblua
打印結果:
/usr/lib/x86_64-linux-gnu/liblua5.1-c++.a
/usr/lib/x86_64-linux-gnu/liblua5.1-c++.so
/usr/lib/x86_64-linux-gnu/liblua5.1-c++.so.0
/usr/lib/x86_64-linux-gnu/liblua5.1-c++.so.0.0.0
/usr/lib/x86_64-linux-gnu/liblua5.1.a
/usr/lib/x86_64-linux-gnu/liblua5.1.so
/usr/lib/x86_64-linux-gnu/liblua5.1.so.0
/usr/lib/x86_64-linux-gnu/liblua5.1.so.0.0.0
所以添加編譯選項:gcc -o add main.c -I /usr/include/lua5.1 -llua5.1
哦也~這下沒有錯誤了
你可以改動add.lua試試,比如在x+y后面再+10! 啊哈, what happened?
amazing script!
