duanjing1023 2014-09-19 21:32
浏览 115

将带有C库的Go程序编译为独立可执行文件

I'm trying to compile a go program to be completely standalone including the c libraries. The command I'm using is

[mm17@mm17 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

So I'll modify the file that calls the grok library to have the following header

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

And then running the same command gives

[mm17@mm17 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

If I then just try to build it normally I also get the same things:

[mm17@mm17 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'
....

I'm sure I'm just doing something stupid, but I can't seem to figure it out, I'm pretty novice when it comes to gcc and go, this is my first non toy program.

  • 写回答

1条回答 默认 最新

  • dty63504 2016-03-11 00:56
    关注

    When you compile native library with GCC make sure to use -fPIC option.

    If library is compiled without -fPIC GO will not be able to locate functions in it.

    评论

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败