在C中調用Lua代碼

來源:互聯網
上載者:User

標籤: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代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.