1. 程式人生 > >lua c api (1)

lua c api (1)

#include <stdio.h> #include <string.h> #include "lua.h" #include "lauxlib.h" #include "lualib.h"

int main() {     char buff[256];     int error ;     lua_State *L = luaL_newstate();     luaL_openlibs(L);     while(fgets(buff, sizeof(buff), stdin) != NULL)     {         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);         }     }     lua_close(L);     return 0; }

編譯:

gcc -o a.out hello.c  -I /usr/local/openresty/luajit/include/luajit-2.1/ -lluajit-5.1

執行

./a.out

輸入lua指令碼

print(“hello word”)

列印

hello word