歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在C中調用Lua代碼

在C中調用Lua代碼

日期:2017/3/1 9:39:46   编辑:Linux編程

這個程序從終端讀入內容,而後按照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'等其他錯誤。

Lua 語言 15 分鐘快速入門 http://www.linuxidc.com/Linux/2013-06/86582.htm

Lua程序設計(第2版)中文 PDF http://www.linuxidc.com/Linux/2013-03/81833.htm

Lua程序設計(第二版)閱讀筆記 http://www.linuxidc.com/Linux/2013-03/81834.htm

NetBSD 將支持用 Lua 腳本開發內核組件 http://www.linuxidc.com/Linux/2013-02/79527.htm

CentOS 編譯安裝 Lua LuaSocket http://www.linuxidc.com/Linux/2011-08/41105.htm

Lua 的詳細介紹:請點這裡
Lua 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved