这个程序从终端读入内容,而后按照lua块执行。
#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(){
char buff[1024];
int error;
memset(buff, 0, sizeof(buff));
lua_State *L = luaL_newstate(); // open lua
luaL_openlibs(L); // open the standard lib
while(fgets(buff, sizeof(buff), stdin) != NULL){
// if success, return 0
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if(error){
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); // pop the err msg from stack
}
}
lua_close(L);
return 0;
}
在编译的时候制定路径,如-I /usr/include/lua5.2/ 或者在include的时候加全include <lua5.2/lua.h> 。此外,要显示的链接lua5.2的库。否则出现 undefined reference to `luaL_newstate‘等其他错误。
原文:http://blog.csdn.net/vonzhoufz/article/details/38927809