linux下安装lua

http://liyaoyi.blog.51cto.com/442933/845296
安装lua:
[root@web ~]# wget http://www.lua.org/ftp/lua-5.2.0.tar.gz
[root@web ~]# tar zxf lua-5.2.0.tar.gz 
[root@web ~]# cd lua-5.2.0
[root@web lua-5.2.0]# vim Makefile 
修改:
INSTALL_TOP= /usr/local/lua
 
[root@web lua-5.2.0]# make
Please do 'make PLATFORM' where PLATFORM is one of these:
   aix ansi bsd freebsd generic linux macosx mingw posix solaris
See doc/readme.html for complete instructions.
#执行make,会提示让你输入make 系统平台
 

luaconf.h:275: fatal error: readline/readline.h: 没有那个文件或目录
则需要安装readline库

解决办法:

apt-get install libreadline5-dev

然后即可完成编译

[root@web lua-5.2.0]# make linux
[root@web lua-5.2.0]# make install
 export PATH=$PATH:/usr/local/lua/bin
[root@web ~]# /usr/local/lua/bin/lua
Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
 

CTRL+C强制退出命令行


gcc -I/usr/local/include/ -L/usr/local/lib/ -lm -DLUA_USE_READLINE a.c /usr/local/lib/liblua.a


辛苦的总算在ubutun中搭建完Lua的开发环境,测试一下Lua的环境
1、测试lua是否安装成功,直接运行lua就能进入编译模式
tao@tao:~/lua-5.2.0$ lua
Lua 5.2.0    Copyright (C) 1994-2011 Lua.org, PUC-Rio
> print("Hello World!")    
Hello World!
>
按ctrl+D退出
 
2、编译lua文件
tao@tao:~/lua-5.2.0$ vi test.lua
在test.lua中输入以下代码后保存
print("-----------------------------");    
print("Hello world");
print("-----------------------------");
在命令行模式下输入以下命令
tao@tao:~/lua-5.2.0$ lua test.lua
-----------------------------
Hello world
-----------------------------
 
3、在C文件中嵌入lua代码
tao@tao:~/lua-5.2.0$ vi a.c
在a.c文件中输入以下代码:
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(int argc, char *argv[])
{
                char line[BUFSIZ];
                lua_State *L = luaL_newstate();
                luaL_openlibs(L);
                //while (fgets(line, sizeof(line), stdin) != 0) printf("%s\n",line);
  luaL_dofile(L, "test.lua");
                lua_close(L);
                return 0;
}
在命令行模式下输入以下命令:
tao@tao:~/lua-5.2.0$ gcc -o a a.c -I/usr/local/include/ -L/usr/local/lib/ -llua -lm -ldl
tao@tao:~/lua-5.2.0$ ./a
-----------------------------
Hello world
-----------------------------
 
编译C文件时要说明的两个问题
1、因为我安装的lua版本是lua-5.2,因为lua-5.2不认识lua_open函数,所以lua_open函数应该由luaL_newstate函数来代替
2、编译选项这里我出现了很多的错误,一会说是lua_open函数不认识,以一会又说pow函数不认识,我在网上找了不少的编译方法,都不见效
(1)方法一,不成功。使用lua_open函数,编译情况如下
tao@tao:~/lua-5.2.0$ gcc -I/usr/local/include/ -L/usr/local/lib/ -llua a.c
a.c: 在函数‘main’中:
a.c:9:24: 警告: 初始化时将整数赋给指针,未作类型转换 [默认启用]
/tmp/cc5nUKNq.o: In function `main':
a.c:(.text+0x23): undefined reference to `lua_open'
a.c:(.text+0x33): undefined reference to `luaL_openlibs'
a.c:(.text+0x6e): undefined reference to `lua_close'
 
(2)方法二,不成功。使用lua_open函数,编译情况如下:
tao@tao:~/lua-5.2.0$ gcc -I/usr/local/include/ -L/usr/local/lib/ a.c /usr/local/lib/liblua.a
a.c: 在函数‘main’中:
a.c:9:24: 警告: 初始化时将整数赋给指针,未作类型转换 [默认启用]
/tmp/ccLLtNhW.o: In function `main':
a.c:(.text+0x23): undefined reference to `lua_open'
/usr/local/lib/liblua.a(lvm.o): In function `luaV_execute':
lvm.c:(.text+0x16d8): undefined reference to `pow'
/usr/local/lib/liblua.a(lobject.o): In function `luaO_arith':
lobject.c:(.text+0x120): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tan':
lmathlib.c:(.text+0x16c): undefined reference to `tan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tanh':
lmathlib.c:(.text+0x1ac): undefined reference to `tanh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sqrt':
lmathlib.c:(.text+0x20e): undefined reference to `sqrt'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sin':
lmathlib.c:(.text+0x23c): undefined reference to `sin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sinh':
lmathlib.c:(.text+0x27c): undefined reference to `sinh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_pow':
lmathlib.c:(.text+0x5d8): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log':
lmathlib.c:(.text+0x6ba): undefined reference to `log'
lmathlib.c:(.text+0x6ca): undefined reference to `log'
lmathlib.c:(.text+0x6ed): undefined reference to `log10'
lmathlib.c:(.text+0x700): undefined reference to `log'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log10':
lmathlib.c:(.text+0x72c): undefined reference to `log10'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_fmod':
lmathlib.c:(.text+0x865): undefined reference to `fmod'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_exp':
lmathlib.c:(.text+0x88c): undefined reference to `exp'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cos':
lmathlib.c:(.text+0x8cc): undefined reference to `cos'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cosh':
lmathlib.c:(.text+0x90c): undefined reference to `cosh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan':
lmathlib.c:(.text+0x94c): undefined reference to `atan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan2':
lmathlib.c:(.text+0x9a8): undefined reference to `atan2'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_asin':
lmathlib.c:(.text+0x9ec): undefined reference to `asin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
lmathlib.c:(.text+0xa2c): undefined reference to `acos'
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0x8e9): undefined reference to `dlsym'
loadlib.c:(.text+0x950): undefined reference to `dlopen'
loadlib.c:(.text+0x981): undefined reference to `dlerror'
loadlib.c:(.text+0x9a1): undefined reference to `dlerror'
/usr/local/lib/liblua.a(loadlib.o): In function `gctm':
loadlib.c:(.text+0xd1c): undefined reference to `dlclose'
 
(3)方法三,不成功。使用lua_open函数,与方法二相比多了-lm,编译情况如下
tao@tao:~/lua-5.2.0$ gcc -I/usr/local/include/ -L/usr/local/lib/ -lm -DLUA_USE_READLINE a.c /usr/local/lib/liblua.a
a.c: 在函数‘main’中:
a.c:9:24: 警告: 初始化时将整数赋给指针,未作类型转换 [默认启用]
/tmp/ccqm5luQ.o: In function `main':
a.c:(.text+0x23): undefined reference to `lua_open'
/usr/local/lib/liblua.a(lvm.o): In function `luaV_execute':
lvm.c:(.text+0x16d8): undefined reference to `pow'
/usr/local/lib/liblua.a(lobject.o): In function `luaO_arith':
lobject.c:(.text+0x120): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tan':
lmathlib.c:(.text+0x16c): undefined reference to `tan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_tanh':
lmathlib.c:(.text+0x1ac): undefined reference to `tanh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sqrt':
lmathlib.c:(.text+0x20e): undefined reference to `sqrt'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sin':
lmathlib.c:(.text+0x23c): undefined reference to `sin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_sinh':
lmathlib.c:(.text+0x27c): undefined reference to `sinh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_pow':
lmathlib.c:(.text+0x5d8): undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log':
lmathlib.c:(.text+0x6ba): undefined reference to `log'
lmathlib.c:(.text+0x6ca): undefined reference to `log'
lmathlib.c:(.text+0x6ed): undefined reference to `log10'
lmathlib.c:(.text+0x700): undefined reference to `log'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_log10':
lmathlib.c:(.text+0x72c): undefined reference to `log10'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_fmod':
lmathlib.c:(.text+0x865): undefined reference to `fmod'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_exp':
lmathlib.c:(.text+0x88c): undefined reference to `exp'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cos':
lmathlib.c:(.text+0x8cc): undefined reference to `cos'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_cosh':
lmathlib.c:(.text+0x90c): undefined reference to `cosh'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan':
lmathlib.c:(.text+0x94c): undefined reference to `atan'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_atan2':
lmathlib.c:(.text+0x9a8): undefined reference to `atan2'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_asin':
lmathlib.c:(.text+0x9ec): undefined reference to `asin'
/usr/local/lib/liblua.a(lmathlib.o): In function `math_acos':
lmathlib.c:(.text+0xa2c): undefined reference to `acos'
/usr/local/lib/liblua.a(loadlib.o): In function `ll_loadfunc':
loadlib.c:(.text+0x8e9): undefined reference to `dlsym'
loadlib.c:(.text+0x950): undefined reference to `dlopen'
loadlib.c:(.text+0x981): undefined reference to `dlerror'
loadlib.c:(.text+0x9a1): undefined reference to `dlerror'
/usr/local/lib/liblua.a(loadlib.o): In function `gctm':
loadlib.c:(.text+0xd1c): undefined reference to `dlclose'
 
 (4)方法四,不成功,使用lua_open函数,与方法三相比增加了-ldl,编译情况如下:
tao@tao:~/lua-5.2.0$ gcc -o a a.c -I/usr/local/include/ -L/usr/local/lib/ -llua -lm -ldl
a.c: 在函数‘main’中:
a.c:9:24: 警告: 初始化时将整数赋给指针,未作类型转换 [默认启用]
/tmp/cctbGHDQ.o: In function `main':
a.c:(.text+0x23): undefined reference to `lua_open'
 (这个我好像可以)
(5)方法五,成功,修改lua_open函数为luaL_newstate函数,再用同样的编译命令编译,成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值