標籤:lua lual_newstate
這個程式從終端讀入內容,而後按照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;}
編譯出現,致命錯誤: lua.h:沒有那個檔案或目錄
locate lua.h,在對應的include目錄下面的確沒有相應的標頭檔,需要下載安裝liblua5.2-dev,而後sudo updatedb;locate lua.h就會找到了。
/home/vonzhou/redis-2.6/deps/lua/doc/lua.html
/home/vonzhou/redis-2.6/deps/lua/etc/lua.hpp
/home/vonzhou/redis-2.6/deps/lua/src/lua.h
/usr/include/lua5.2/lua.h
/usr/include/lua5.2/lua.hpp
/usr/src/linux-headers-3.2.0-23-generic/include/config/scsi/dh/alua.h
在編譯的時候制定路徑,如-I /usr/include/lua5.2/ 或者在include的時候加全include <lua5.2/lua.h> 。此外,要顯示的連結lua5.2的庫。否則出現 undefined reference to `luaL_newstate‘等其他錯誤。
在C中調用Lua代碼