首次用c调度lua的时候,按照习惯会使用:
gcc -o a main.c
不过会报错,因为缺少链接库
使用:gcc -o a main.c -llua -lm -ldl
基本就可以了
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
void load(lua_State *L,const char *fname,int *a ,int *b);
int main()
{
const char *filename = "file.lua";
int w,v;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
load(L,filename,&w,&v);
lua_close(L);
printf("get width : %d and height : %d \n",w,v);
return 0;
}
void load(lua_State *L,const char *fname,int *a ,int *b)
{
if(luaL_loadfile(L,fname) || lua_pcall(L,0,0,0))
{
printf("can not load the file info :%s",lua_tostring(L,-1));
}
lua_getglobal(L,"width");
lua_getglobal(L,"height");
if(!lua_isnumber(L,-2))
{
printf("can not get width");
}
if(!lua_isnumber(L,-1))
{
printf("can not get height");
}
*a = lua_tonumber(L,-2);
*b = lua_tonumber(L,-1);
}
file.lua文件
width = 20
height = 300