Linux下使用靜態連結方式如何使用Lua

來源:互聯網
上載者:User

Linux下使用靜態連結方式如何使用Lua是本文要介紹的內容,主要是來學習在linux中使用lua的使用方式,具體內容的實現來看本文詳解。

環境是ubuntu9.04

首先去lua官方網站下載lua最新版

解壓縮之後,從shell中進入目錄,然後執行make

會給個提示,選擇make的版本

因為是linux下,就打make linux

然後開始構建了,等下就好,進入src目錄,裡面有liblua.a

拷貝到項目需要的目錄。

同時需要拷貝的是.h我建議把那標頭檔都拷貝到/usr/include下,這樣以後用的時候方便,要不每次還得在gcc中加命令列

在gcc中指定標頭檔參見:

 
  1. http://blog.chinaunix.net/u/28781/showart.php?id=401631 

然後寫好代碼就可以編譯了。我的測試代碼是:

代碼

 
  1.  void load (char *filename, int *width, int *height) {  
  2.  
  3.      lua_State *L = lua_open();  
  4.     luaopen_base(L);  
  5.      luaopen_io(L);  
  6.      luaopen_string(L);  
  7.     luaopen_math(L);  
  8.     if (luaL_loadfile(L, filename) || lua_pcall(L, 0, 0, 0))  
  9.        error(L, "cannot run configuration file: %s",  
  10.            lua_tostring(L, -1));  
  11.     lua_getglobal(L, "width");   
  12.     lua_getglobal(L, "height");   
  13.     if (!lua_isnumber(L, -2))   
  14.        error(L, "`width' should be a number\n");   
  15.     if (!lua_isnumber(L, -1))  
  16.         error(L, "`height' should be a number\n");   
  17.     *width = (int)lua_tonumber(L, -2);   
  18.     *height = (int)lua_tonumber(L, -1);  
  19.     lua_close(L);   
  20. }  
  21. int main()  
  22. {  
  23.     return 0;  

編譯的命令列是:

 
  1. gcc test.c -L$HOME/Code/luatest -llua -lm  

需要注意,gcc不用在靜態庫前加lib,得到的是liblua。。我剛開始編譯的時候總提示找不到,後來在群裡請教了一下大牛,解決了

還有-lm也是必須的,因為lua需要用到libm這個庫。

小結:Linux下使用靜態連結方式如何使用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.