C语言调用Lua编译问题总结

来源:互联网 发布:php登录页面代码 编辑:程序博客网 时间:2024/06/10 03:49

正确的编译命令

gcc -lm  -g -o test test.c /usr/local/lib/liblua.a -ldl​

问题1:缺少/usr/local/lib/liblua.a

/data1/www/lua/luacapi/testlua3.c:10: undefined reference to `luaL_newstate'/data1/www/lua/luacapi/testlua3.c:11: undefined reference to `luaL_openlibs'/data1/www/lua/luacapi/testlua3.c:15: undefined reference to `luaL_loadstring'/data1/www/lua/luacapi/testlua3.c:15: undefined reference to `lua_pcallk'/data1/www/lua/luacapi/testlua3.c:18: undefined reference to `lua_tolstring'/data1/www/lua/luacapi/testlua3.c:19: undefined reference to `lua_settop'/data1/www/lua/luacapi/testlua3.c:22: undefined reference to `lua_close'

问题2:缺少-lm参数

/usr/local/lib/liblua.a(lobject.o): In function `numarith': lobject.c:(.text+0xc8d): undefined reference to `fmod'  lobject.c:(.text+0xcb8): undefined reference to `pow'   /usr/local/lib/liblua.a(lvm.o): In function `luaV_execute': lvm.c:(.text+0x2068): undefined reference to `pow'  lvm.c:(.text+0x211c): undefined reference to `fmod' /usr/local/lib/liblua.a(lmathlib.o): In function `math_log10':  lmathlib.c:(.text+0x21e): undefined reference to `log10'    /usr/local/lib/liblua.a(lmathlib.o): In function `math_pow':    lmathlib.c:(.text+0x338): undefined reference to `pow'  ........​

问题3:缺少-ldl参数​

/usr/local/lib/liblua.a(loadlib.o): In function `lookforfunc':  loadlib.c:(.text+0x748): undefined reference to `dlsym'loadlib.c:(.text+0x7b3): undefined reference to `dlopen'loadlib.c:(.text+0x851): undefined reference to `dlerror'loadlib.c:(.text+0x871): undefined reference to `dlerror'/usr/local/lib/liblua.a(loadlib.o): In function `gctm':loadlib.c:(.text+0xa44): undefined reference to `dlclose'​

问题解析​​

1、为什么会出现undefined reference to ‘xxxxx’错误?​

首先这是链接错误,不是编译错误,也就是说如果只有这个错误,说明你的程序源码本身没有问题,是你用编译器编译时参数用得不对,你没有指定链接程序要用到得库,比如你的程序里用到了一些数学函数,那么你就要在编译参数里指定程序要链接数学库,方法是在编译命令行里加入-lm。

2、-l参数和-L参数

-l参数 就是用来指定程序要链接的库,-l参数紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,很容易看出,把库文件名的头lib和尾.so去掉就是库名了。好了现在我们知道怎么得到库名了,比如我们自已要用到一个第三方提供的库名字叫libtest.so,那么我们只要把libtest.so拷贝到/usr/lib里,编译时加上-ltest参数,我们就能用上libtest.so库了(当然要用libtest.so库里的函数,我们还需要与libtest.so配套的头文件)。放在/lib和/usr/lib和/usr/local/lib里的库直接用-l参数就能链接了,但如果库文件没放在这三个目录里,而是放在其他目录里,这时我们只用-l参数的话,链接还是会出错,出错信息大概是:“/usr/bin/ld: cannot find -lxxx”,也就是链接程序ld在那3个目录里找不到libxxx.so,这时另外一个参数-L就派上用场了,比如常用的X11的库,它放在/usr/X11R6/lib目录下,我们编译时就要用-L/usr/X11R6/lib -lX11参数,-L参数跟着的是库文件所在的目录名。再比如我们把libtest.so放在/aaa/bbb/ccc目录下,那链接参数就是-L/aaa/bbb/ccc -ltest另外,大部分libxxxx.so只是一个链接,以RH9为例,比如libm.so它链接到/lib/libm.so.x,/lib/libm.so.6又链接到/lib/libm-2.3.2.so,如果没有这样的链接,还是会出错,因为ld只会找libxxxx.so,所以如果你要用到xxxx库,而只有libxxxx.so.x或者libxxxx-x.x.x.so,做一个链接就可以了ln -s libxxxx-x.x.x.so libxxxx.so​

参考资料:​
http://blog.csdn.net/uisoul/article/details/60135764
http://blog.csdn.net/uisoul/article/details/60135383
http://blog.csdn.net/uisoul/article/details/60135764

0 0