写这篇文章时,参考了如下
http://www.cnblogs.com/pied/archive/2012/10/26/2741601.html点击打开链接
http://blog.csdn.net/puppet_master/article/details/47980401点击打开链接
1、下载lua
curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
mv lua-5.3.4.tar.gz /opt
cd /opt
tar zxf lua-5.3.4.tar.gz
cd lua-5.3.4
make linux test
2、如果编译报错,readline/readline.h: No such file or directory
针对debian: apt-get install libreadline-dev
针对redhat:dnf install readline-devel
3、熟悉一下gcc的基本命令
-o filename 输出文件名
-I dirpath 头文件路径
-L dirpath 库路径
-l libname 库名,就是文件名去掉lib,so剩下的那部分
在这里,我们需要用到lua.h这个头文件,以及动态库或者静态库任选一种,但我们首先需要确定它们的具体路径,如果已经知道,可以略过
sudo updatedb
locate lua.h #查找头文件
locate liblua #查找库
--test.lua
function add(x, y)
return x + y
end
5、再了解一下需要用到的c函数,基本如下
//检查栈中index索引的数据类型是否是*的类型
int lua_is*(lua_State * L, int index)
//返回栈中index索引的数据的类型
int lua_type(lua_State* L, int index)
//返回栈中index索引的数据的值,转化为*的类型
* lua_to*(lua_State* L, int index)
//向栈中插入*类型的元素
void lua_push*(lua_State* L, type*)
//获得栈中元素个数
int lua_gettop(lua_gettop) (lua_State *L);
//设置栈顶为一个指定位置
void lua_settop(lua_settop) (lua_State *L, int idx);
//将指定索引上的值再次压入栈
void lua_pushvalue(lua_State *L, int idx);
//删除指定索引的元素,之上的向下移补缺
void lua_remove(lua_State* L, int idx);
//在index处开辟一个位置,上面的上移,然后将栈顶元素放到这个位置
void lua_insert(lua_State* L, int idx);
//弹出栈顶元素,使用该元素替代index元素
void lua_replace(lua_State* L, int idx);
lua_State是一个栈结构
6、c文件
//test.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();
L = luaL_newstate();
/*load Lua base libraries*/
luaL_openlibs(L);
/*load the script*/
luaL_dofile(L, "test.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;
}
7、编译运行,假设使用动态库,如果我们locate的结果动态库如下
很显然,动态库名为lua-5.3
所以,编译命令为
gcc -I/opt/lua-5.3.4/src -llua-5.3 -o test test.c
gcc -o test1 test.c -I/opt/lua-5.3.4/src -L /opt/lua-5.3.4/src -l lua -l m -l dl
可以看到,静态库包括lua,m ,dl库。
运行,已经可以看到add的结果了
8、稍微修改一下,调用lua一个返回string的函数
在test.lua添加
function getstring()
return "hello lua"
end
在test.c中添加
char* luagetstring()
{
lua_getglobal(L, "getstring");
lua_call(L, 0, 1);
char* str = (char*) lua_tostring(L, -1);
lua_pop(L, 1);
return str;
}
在test.c中main函数添加
char* str = luagetstring();
printf("string is %s\n", str);
9、得到lua的全局变量
在test.lua中添加
name = "zhangsan"
char* luafield()
{
lua_getglobal(L, "name");
char* str = (char*) lua_tostring(L, -1);
lua_pop(L, 1);
return str;
}
主函数调用,
str = luafield();
printf("field is %s\n", str);
10、得到table中值
在test.lua添加
stu = {}
stu.age = 23
stu.school = "ncu"
在test.c添加
void luatable()
{
lua_getglobal(L, "stu");
lua_getfield(L, -1, "school");
char* str = (char*) lua_tostring(L, -1);
printf("school is %s\n", str);
lua_getfield(L, -1, "age");
int age = (int)lua_tonumber(L, -1);
printf("age is %d\n", age);
lua_pop(L, 1);
}
主函数调用,得到结果。
上面的lua_call可以改成lua_pcall(L,0,0,0),类似这种,如果发生错误,栈顶就是错误信息
至此,c语言调用lua已经可以完成了。