代码改变世界

我的第一个Lua程序[转]

2012-09-10 16:03  tetang1230  阅读(638)  评论(0编辑  收藏  举报
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://cynthia.blog.51cto.com/839408/850565
我的第一个Lua程序
辛苦的总算在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函数,再用同样的编译命令编译,成功
 
 
多查资料,多试验,相信自己一定可以学好Lua的,唉,领导给的任务,好好学习

本文出自 “豆豆熊的快乐IT人生” 博客,请务必保留此出处http://cynthia.blog.51cto.com/839408/850565