lua 首次编译

首次用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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值