2013年(6)
分类: LINUX
2013-04-27 20:14:05
原文地址:lua c 编译问题解决 作者:wangchenxicool
安装lua 5.1.2
tar zxf lua5.1.2
cd lua5.1.2
make freebsd
make install
然后写一个代码
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char line[BUFSIZ];
lua_State *L = lua_open();
luaL_openlibs(L);
while (fgets(line, sizeof(line), stdin) != 0) printf("%s\n",line);
lua_close(L);
return 0;
}
====================
按照官方文档
luaopen_base(L); /* opens the basic library */
luaopen_table(L); /* opens the table library */
luaopen_io(L); /* opens the I/O library */
luaopen_string(L); /* opens the string lib. */
luaopen_math(L); /* opens the math lib. */
全部用一个函数取代:luaL_openlibs(L);
接着执行编译. 习惯性的。用:
gcc -I/usr/local/include/ -L/usr/local/lib/ -llua a.c
报一大堆错误:
/var/tmp//ccWOsUaY.o(.text+0x20): In function `main':
: undefined reference to `luaL_newstate'
/var/tmp//ccWOsUaY.o(.text+0x34): In function `main':
: undefined reference to `luaL_openlibs'
/var/tmp//ccWOsUaY.o(.text+0x7f): In function `main':
: undefined reference to `lua_close'
再改一下进行编译:
gcc -I/usr/local/include/ -L/usr/local/lib/ a.c /usr/local/lib/liblua.a
/usr/local/lib/liblua.a(lvm.o)(.text+0xa0e): In function `Arith':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lvm.o)(.text+0xa3e): In function `Arith':
: undefined reference to `pow'
/usr/local/lib/liblua.a(lvm.o)(.text+0x1b3d): In function `luaV_execute':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lvm.o)(.text+0x1cd0): In function `luaV_execute':
: undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x3e): In function `math_sin':
: undefined reference to `sin'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x6e): In function `math_sinh':
: undefined reference to `sinh'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x9e): In function `math_cos':
: undefined reference to `cos'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0xce): In function `math_cosh':
: undefined reference to `cosh'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0xfe): In function `math_tan':
: undefined reference to `tan'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x12e): In function `math_tanh':
: undefined reference to `tanh'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x15e): In function `math_asin':
: undefined reference to `asin'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x18e): In function `math_acos':
: undefined reference to `acos'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x1be): In function `math_atan':
: undefined reference to `atan'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x1fb): In function `math_atan2':
: undefined reference to `atan2'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x22e): In function `math_ceil':
: undefined reference to `ceil'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x25e): In function `math_floor':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x29b): In function `math_fmod':
: undefined reference to `fmod'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x31e): In function `math_sqrt':
: undefined reference to `sqrt'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x35b): In function `math_pow':
: undefined reference to `pow'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x38e): In function `math_log':
: undefined reference to `log'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x3be): In function `math_log10':
: undefined reference to `log10'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x3ee): In function `math_exp':
: undefined reference to `exp'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x66e): In function `math_random':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lmathlib.o)(.text+0x6bb): In function `math_random':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lcode.o)(.text+0xf3f): In function `codearith':
: undefined reference to `floor'
/usr/local/lib/liblua.a(lcode.o)(.text+0xf70): In function `codearith':
: undefined reference to `pow'
一时不得其解. 这些函数liblua.a包里都有了,怎么会还说未定义呢? 后来突然想起,lua里直接调用了标准库里的c API,因此还要加上 数学库
于是这么干:
gcc -I/usr/local/include/ -L/usr/local/lib/ -lm -DLUA_USE_READLINE a.c /usr/local/lib/liblua.a
BINGO!!!成功了。
即加上 -lm 这个数学库. 也许很多人编译时,在其他库中已经连接了这个-lm数学库,但如果很干净的代码,未连接这个库,那么hello world的入门,估计也编译无法通过。
初学lua, 笔记下,免得以后遇到同样问题的人郁闷。