关于go语言使用lua的问题

435 views
Skip to first unread message

linzhiyu

unread,
May 15, 2013, 10:51:27 PM5/15/13
to golang-china订阅邮件组
好久没来麻烦大家了,今天不得已请教几个问题:
刚学完lua,想在go里面使用。搜索到https://github.com/aarzilli/golua这个包,然后导入,结果与项目一起编译时,出现提示:
ld: library not found for -llua
collect2: ld returned 1 exit status

请问:
1、我没有使用上面所说的go get方式,而是直接下载zip包,然后放在项目中编译。这样有问题么?
2、需要在服务器上先安装lua相关的什么软件么?
3、有没有其他更简单快捷的在go中使用lua的方法?比如其他的包或者使用方式。

多谢赐教!


chai2010

unread,
May 15, 2013, 10:54:51 PM5/15/13
to golang...@googlegroups.com
lua是纯C写的吧, 直接把代码扔到同一个目录, cgo应该可以自动识别的.



--
--
官网: http://golang-china.org/
IRC: irc.freenode.net #golang-china
@golangchina
 
---
您收到此邮件是因为您订阅了 Google 网上论坛的“Golang-China”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 golang-china...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 



--
chaishushan
http://my.oschina.net/chai2010

Xing Xing

unread,
May 15, 2013, 11:01:00 PM5/15/13
to golang...@googlegroups.com, lzy...@163.com
于 Thu, 16 May 2013 10:51:27 +0800 (CST)
linzhiyu <lzy...@163.com> 写道:

> 出现提示:
> ld: library not found for -llua
> collect2: ld returned 1 exit status

export CGO_CFLAGS="-I/usr/include/lua"
export CGO_LDFLAGS="-llua"

头文件的地址根据你具体环境替换一下。


>
> 请问:
> 1、我没有使用上面所说的go
> get方式,而是直接下载zip包,然后放在项目中编译。这样有问题么?

只要 GOPATH 一致就不会有问题

> 2、需要在服务器上先安装lua相关的什么软件么?

lua 相关的头文件和 so 是必须得装的。例如 ubutnu 通常是 lua-dev
之类的包。

> 3、有没有其他更简单快捷的在go中使用lua的方法?比如其他的包或者使用方式。

这就不是十分清楚了,现在还是使用 golua 的居多吧。基本都是 cgo 调用 c
的接口的方式。如果有人用 go 写个 interpreter 那就爽歪歪了。 :)

>
>
> 多谢赐教!
>

linzhiyu

unread,
May 15, 2013, 11:04:50 PM5/15/13
to Xing Xing, golang...@googlegroups.com
原来如此,多谢多谢

linzhiyu

unread,
May 15, 2013, 11:05:08 PM5/15/13
to golang...@googlegroups.com
代码是在项目的src/Lib/里面
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到 golang-china+unsub...@googlegroups.com
要查看更多选项,请访问 https://groups.google.com/groups/opt_out。
 
 


linzhiyu

unread,
May 15, 2013, 11:08:49 PM5/15/13
to Xing Xing, golang...@googlegroups.com
==,星星大的意思是/usr/include/lua这个地方要安装一个lua相关的东西?还是在哪个文件中添加那两句?C都快忘光了,还请指点。

在 2013-05-16 11:01:00,"Xing Xing" <mike...@gmail.com> 写道:

linzhiyu

unread,
May 15, 2013, 11:15:27 PM5/15/13
to golang...@googlegroups.com
主要是对cgo没有任何了解。。。只是为了前后台某些公式可以对应,然后后台如果有修改,可以通过字符串脚本的形式传给前台。前台是ios的。

星大觉得用lua、js、python的哪个比较好呢?前提是对于cgo完全不了解。希望能直接嵌入go编译的那种。求推荐。


在 2013-05-16 11:01:00,"Xing Xing" <mike...@gmail.com> 写道:

wxf

unread,
May 16, 2013, 10:33:28 AM5/16/13
to golang...@googlegroups.com
替xing大回复一下,在golua/lua/lua.go中,有下面的一些内容,是制定包含的库和头文件的,根据自己的情况可以适当改一下。
/*
#cgo CFLAGS: -Ilua
#cgo llua LDFLAGS: -llua
#cgo linux,!llua LDFLAGS: -llua5.1
#cgo darwin LDFLAGS: -llua
#cgo freebsd LDFLAGS: -llua

#include <lua.h>
#include <stdlib.h>

#include "golua.h"

*/

之前我参考golua写过一个自己的封装,在code.google.com/p/glua,你可以做一下参考。

linzhiyu

unread,
May 16, 2013, 9:16:53 PM5/16/13
to golang...@googlegroups.com
非常感谢,我研究一下先。

linzhiyu

unread,
May 16, 2013, 11:12:01 PM5/16/13
to golang...@googlegroups.com
正式在我mac系统上安装完成lua5.2.1版本。

结果go编译第一个包,提示的是:
# Lib/golua/lua
Undefined symbols for architecture x86_64:
"_lua_tonumber", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_tonumber in golua.cgo2.o
(maybe you meant: _lua_tonumberx, __cgo_7bf27e0e26b2_Cfunc_lua_tonumber )
"_lua_tointeger", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_tointeger in golua.cgo2.o
_clua_atpanic in c-golua.o
_callback_panicf in c-golua.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_tointeger, _lua_tointegerx )
"_luaL_loadfile", referenced from:
__cgo_7bf27e0e26b2_Cfunc_luaL_loadfile in lauxlib.cgo2.o
(maybe you meant: _luaL_loadfilex, __cgo_7bf27e0e26b2_Cfunc_luaL_loadfile )
"_lua_yield", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_yield in lua.cgo2.o
(maybe you meant: _lua_yieldk, __cgo_7bf27e0e26b2_Cfunc_lua_yield )
"_lua_setfenv", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_setfenv in lua.cgo2.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_setfenv)
"_lua_pcall", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_pcall in lua.cgo2.o
(maybe you meant: _lua_pcallk, __cgo_7bf27e0e26b2_Cfunc_lua_pcall )
"_lua_objlen", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_objlen in lua.cgo2.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_objlen)
"_lua_lessthan", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_lessthan in lua.cgo2.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_lessthan)
"_lua_getfenv", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_getfenv in lua.cgo2.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_getfenv)
"_lua_equal", referenced from:
__cgo_7bf27e0e26b2_Cfunc_lua_equal in lua.cgo2.o
(maybe you meant: __cgo_7bf27e0e26b2_Cfunc_lua_equal)
"_lua_call", referenced from:
_clua_openos in c-golua.o
_clua_opentable in c-golua.o
_clua_openstring in c-golua.o
_clua_openpackage in c-golua.o
_clua_openmath in c-golua.o
_clua_openio in c-golua.o
_clua_openbase in c-golua.o
...
(maybe you meant: _lua_callk)
ld: symbol(s) not found for architecture x86_64

collect2: ld returned 1 exit status


编译您的那个包,提示的是:
# Lib/glua
error: 'lua_tointeger' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: 'luaL_loadfile' undeclared (first use in this function)
error: 'lua_tonumber' undeclared (first use in this function)
error: 'lua_pcall' undeclared (first use in this function)
error: 'lua_call' undeclared (first use in this function)
exit status 2

请指教。


在 2013-05-16 22:33:28,wxf <wang...@gmail.com> 写道:

wxf

unread,
May 17, 2013, 6:48:15 AM5/17/13
to golang...@googlegroups.com
是用的版本不对,或者库没有找到?
我的只测试过luajit5.1的版本,lua5.2没试过。

amanout

unread,
May 17, 2013, 10:17:47 AM5/17/13
to Golang-China
编译时指定Lua 头文件目录 和 库目录
go build -gcflags "-I D:\clib\lua\include" -ldflags "-L D:\clib\lua
\lib" mypkg

linzhiyu

unread,
May 20, 2013, 4:25:24 AM5/20/13
to golang...@googlegroups.com
我在http://luajit.org/里面只发现luajit2.0.0和2.0.1,所谓的luajit5.1是哪来的?

linzhiyu

unread,
May 20, 2013, 4:27:55 AM5/20/13
to golang...@googlegroups.com
glua的原包设定是
#cgo CFLAGS: -I /usr/local/include/luajit-2.0
#cgo LDFLAGS: -l luajit-5.1

我现在用的是lua5.2.1,luajit2.0.1
该怎么修改这个设定?该来该去都不对

Ruiqi Hong

unread,
May 20, 2013, 4:32:35 AM5/20/13
to golang...@googlegroups.com
你到/usr/local/lib下ls -l|grep luajit看看so的文件名是什么,libluajit-5.1.so对应的LDFLAGS就是-l luajit-5.1

linzhiyu

unread,
May 20, 2013, 4:37:29 AM5/20/13
to golang...@googlegroups.com
-rwxr-xr-x 1 root wheel 471992 5 20 16:10 libluajit-5.1.2.0.1.dylib
lrwxr-xr-x 1 root wheel 25 5 20 16:10 libluajit-5.1.2.dylib -> libluajit-5.1.2.0.1.dylib
-rw-r--r-- 1 root wheel 706392 5 20 16:10 libluajit-5.1.a
lrwxr-xr-x 1 root wheel 25 5 20 16:10 libluajit-5.1.dylib -> libluajit-5.1.2.0.1.dylib

只有这些文件耶

Ruiqi Hong

unread,
May 20, 2013, 4:40:11 AM5/20/13
to golang...@googlegroups.com
哦,没注意到是mac,这样的话-lluajit-5.1就行吧

linzhiyu

unread,
May 20, 2013, 5:06:47 AM5/20/13
to golang...@googlegroups.com
为什么
#cgo LDFLAGS: -l luajit-5.1 编译不通过
#cgo LDFLAGS: -lluajit-5.1编译就通过了?

Frey

unread,
May 20, 2013, 5:48:44 AM5/20/13
to golang...@googlegroups.com
gcc参数就是这样 -l后面直接连上文件名 不要空格

wxf

unread,
May 20, 2013, 9:51:21 AM5/20/13
to golang...@googlegroups.com
不好意思,没说明白。

luajit是实现的lua5.1版本的标准,接口兼容,但lua5.2和5.1的接口有变化。
所以,luajit2.0和lua5.1的接口兼容,最好使用这个版本的库。

其次,编译时,应使用动态库进行连接,如:linux上是*.so的库。

另外,根据编译的结果,如果说编译的出来的库是libluajit-5.1.so,那么就用#cgo LDFLAGS: -l luajit-5.1,编译出来时libluajit.so,那么就是#cgo LDFLAGS: -l luajit

#cgo CFLAGS: -I /usr/local/include/luajit-2.0这个是设置lua.h等头文件的目录。


在 2013年5月20日下午4:25,linzhiyu <lzy...@163.com>写道:

minux

unread,
May 20, 2013, 10:15:52 AM5/20/13
to golang...@googlegroups.com

2013/5/20 linzhiyu <lzy...@163.com>

为什么
#cgo LDFLAGS: -l luajit-5.1 编译不通过
#cgo LDFLAGS: -lluajit-5.1编译就通过了?
确实是个 bug。CL 9416047。

linzhiyu

unread,
May 20, 2013, 9:19:11 PM5/20/13
to golang...@googlegroups.com
好吧,对gcc还不够了解,看前面-I是有空格的,但是l又没有,所以纳闷,多谢指点。

linzhiyu

unread,
May 20, 2013, 9:21:51 PM5/20/13
to golang...@googlegroups.com
我的是mac系统,突然多了一个libluajit-5.1.2.0.1.dylib这个文件,不知道是不是编译后的。。。

linzhiyu

unread,
May 20, 2013, 9:33:02 PM5/20/13
to golang...@googlegroups.com
另外,是直接把glua包放在项目中用goinstall编译就可以了,还是需要额外gcc来编译呀?


在 2013-05-20 21:51:21,wxf <wang...@gmail.com> 写道:

Frey

unread,
May 21, 2013, 8:39:03 AM5/21/13
to golang...@googlegroups.com
用到cgo的要安装gcc/mingw/cygwin

minux

unread,
May 21, 2013, 8:41:13 AM5/21/13
to golang...@googlegroups.com

2013/5/21 Frey <zmy...@gmail.com>
用到cgo的要安装gcc/mingw/cygwin
用 cgo 不需要 cygwin。在 Windows 上只需要 mingw gcc 就行了。 

wxf

unread,
May 21, 2013, 9:41:37 AM5/21/13
to golang...@googlegroups.com
glua直接使用go install即可。


在 2013年5月21日上午9:33,linzhiyu <lzy...@163.com>写道:

linzhiyu

unread,
May 21, 2013, 9:31:13 PM5/21/13
to golang...@googlegroups.com
已经搞定,忘记看守护进程打印的另外一个日志了。。。多谢大家的答疑
Reply all
Reply to author
Forward
0 new messages