C调用Lua -- 简单的解释器程序实现

来源:互联网 发布:王者传奇手游转生数据 编辑:程序博客网 时间:2024/06/15 03:18

C调用Lua – 简单的解释器程序实现

  • C调用Lua 简单的解释器程序实现

November 5, 2015 10:57 PM

仿照*《Lua程序设计第二版》*ch24中的示例程序,在Lua 5.3.1版本下成功利用gcc编译运行了这段代码。

首先源代码程序如下

#include <stdio.h>#include <string.h>#include "lua.h"#include "lauxlib.h"#include "lualib.h"int main(void){    char buff[256];    int error;    lua_State *L = luaL_newstate();             /*opens lua*/    luaL_openlibs(L);                       /*opens the standard libraries*/    while(fgets(buff, sizeof(buff), stdin) != NULL) {        error = luaL_loadstring(L, buff) || lua_pcall(L, 0, 0, 0);        if(error) {            fprintf(stderr, "%s\n", lua_tostring(L, -1));            lua_pop(L, 1);                  /* pop error message from the stack */        }    }    lua_close(L);    return 0;}

在编译链接阶段出现了好多问题,总是有undefined reference to ...的错误出现。
最终使用gcc 24-1.c -o 24-1.exe -llua -lm -ldl命令成功编译,运行程序成功进入Lua环境。
至于-llua, -lm, -ldl选项的作用,请自行google。

0 0
原创粉丝点击