2014-09-19 79 views
3

我试图编译一个go程序以完全独立,包括c库。我使用的命令是用C库编译一个Go程序到一个独立的可执行文件

[[email protected] grogger]$ go build --ldflags '-extldflags "-static"' ./grogger.go 
# command-line-arguments 
/usr/bin/ld: cannot find -lgrok 
collect2: ld returned 1 exit status 
/home/mm17/go/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit status 0x100 

所以我会修改调用神交库有以下标题

1 package grok 
    2 
    3 /* 
    4 #cgo LDFLAGS: -L /usr/lib/libgrok.so -lgrok 
    5 #include <grok.h> 
    6 */ 
    7 import "C" 

,然后运行相同的命令给

[[email protected] grogger]$ go build --ldflags '-extldflags "-static"' ./grogger.go 
# github.com/blakesmith/go-grok 
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400180 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_strlen': 
../../blakesmith/go-grok/grok.go:169: undefined reference to `strlen' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_patterns_import_from_file': 
../../blakesmith/go-grok/grok.go:159: undefined reference to `grok_patterns_import_from_file' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_pattern_add': 
../../blakesmith/go-grok/grok.go:147: undefined reference to `grok_pattern_add' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_new': 
../../blakesmith/go-grok/grok.go:132: undefined reference to `grok_new' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_next': 
../../blakesmith/go-grok/grok.go:123: undefined reference to `grok_match_walk_next' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_exec': 
../../blakesmith/go-grok/grok.go:81: undefined reference to `grok_exec' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_discover_new': 
../../blakesmith/go-grok/grok.go:68: undefined reference to `grok_discover_new' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_compile': 
../../blakesmith/go-grok/grok.go:46: undefined reference to `grok_compile' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_init': 
../../blakesmith/go-grok/grok.go:108: undefined reference to `grok_match_walk_init' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_match_walk_end': 
../../blakesmith/go-grok/grok.go:99: undefined reference to `grok_match_walk_end' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_free': 
../../blakesmith/go-grok/grok.go:90: undefined reference to `grok_free' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_grok_discover': 
../../blakesmith/go-grok/grok.go:58: undefined reference to `grok_discover' 
/tmp/go-build731085030/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_free': 
../../blakesmith/go-grok/grok.go:34: undefined reference to `free' 
collect2: ld returned 1 exit status 
文件

如果我只是试图建立它通常我也会得到同样的东西:

[[email protected] grogger]$ go build grogger.go 
# github.com/blakesmith/go-grok 
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400180 
/tmp/go-build294631148/github.com/blakesmith/go-grok/_obj/grok.cgo2.o: In function `_cgo_4244208c3352_Cfunc_strlen': 
../../blakesmith/go-grok/grok.go:169: undefined reference to `strlen' 
.... 

我确定我只是在做一些愚蠢的事情,但我似乎无法弄清楚,当谈到gcc并且走了,我是新手,这是我的第一个非玩具程序。

+0

在LDFLAGS中添加'-lc'是否有帮助? '#cgo LDFLAGS:-L /usr/lib/libgrok.so -lgrok -lc'? – OneOfOne 2014-09-19 22:02:33

+0

没有,仍然是相同的结果 – GardenMWM 2014-09-19 22:33:23

+1

你的库路径中是否有libgrok.a?您无法从共享库中创建静态可执行文件。 – JimB 2014-09-20 00:38:09

回答

0

当您使用GCC编译本机库时,请务必使用-fPIC选项。

如果库编译时没有-fPIC GO将无法在其中找到函数。

相关问题