How to Use Lua using static links in Linux

Source: Internet
Author: User

LinuxUseStaticHow to use the linkLuaIs the content to be introduced in this article, mainly to learnLinuxUsed inLuaFor more information, see the detailed description of this article.

The environment is ubuntu9.04.

First, go to the lua official website to download the latest version of lua.

After decompression, enter the directory from the shell and execute make

You will be prompted to select the make version.

Make linux

Then start building. Wait and enter the src directory, which contains liblua..

Copy to the directory required by the project.

At the same time, we need to copy the. h file. I suggest copying all the header files to the/usr/include file for later use. Otherwise, we need to add the command line in gcc each time.

To specify a header file in gcc, see:

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

Then you can compile the code. My test code is:

Code

 
 
  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;  

The compiled command line is:

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

Note that gcc does not needStaticAdd lib to the front of the library to obtain liblua .. At the beginning of compilation, I always asked why I couldn't find it. Then I asked Daniel in the group to solve the problem.

-Lm is also required, because lua needs to use the libm library.

Summary:LinuxUseStaticHow to use the linkLuaI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.