打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
go语言动态库的编译和使用

go语言动态库的编译和使用

本文主要介绍go语言动态库的编译和使用方法,以linux平台为例,windows平台步骤一样,具体环境如下:

$ echo $GOPATH/media/sf_share/git/go_practice$ echo $GOROOT/usr/lib/golang/$ tree $GOPATH/src/media/sf_share/git/go_practice/src|-- demo|   `-- demo.go`-- main.go1 directory, 2 files

在$GOPATH/src目录,有demo包和使用demo包的应用程序main.go,main.go代码如下:

package mainimport "demo"func main() {    demo.Demo()}

demo包中的demo.go代码如下:

package demoimport "fmt"func Demo() {    fmt.Println("call demo ...")}

由于demo.go是$GOPATH/src目录下的一个包,main.go在import该包后,可以直接使用,运行main.go:

$ go run main.gocall demo ...

现在,需要将demo.go编译成动态库libdemo.so,让main.go以动态库方式编译,详细步骤如下:

1 将go语言标准库编译成动态库

$ go install -buildmode=shared -linkshared  std

在命令行运行go install -buildmode=shared -linkshared std命令,-buildmode指定编译模式为共享模式,-linkshared表示链接动态库,成功编译后会在$GOROOT目录下生标准库的动态库文件libstd.so,一般位于$GOROOT/pkg/linux_amd64_dynlink目录:

$ cd $GOROOT/pkg/linux_amd64_dynlink$ ls libstd.solibstd.so

2 将demo.go编译成动态库

$ go install  -buildmode=shared -linkshared demo$ cd $GOPATH/pkg$ ls linux_amd64_dynlink/demo.a  demo.shlibname  libdemo.so

成功编译后会在$GOPATH/pkg目录生成相应的动态库libdemo.so。

3 以动态库方式编译main.go

$ go build -linkshared main.go$ ll -htotal 25Kdrwxrwx---. 1 root vboxsf 4.0K Apr 28 17:30 ./drwxrwx---. 1 root vboxsf 4.0K Apr 28 17:22 ../drwxrwx---. 1 root vboxsf    0 Apr 28 08:37 demo/-rwxrwx---. 1 root vboxsf  16K Apr 28 17:30 main*-rwxrwx---. 1 root vboxsf   58 Apr 28 08:37 main.go*$ ./maincall demo ...

从示例中可以看到,以动态库方式编译生成的可执行文件main大小才16K。如果以静态库方式编译,可执行文件main大小为1.5M,如下所示:

$ go build main.go$ ll -htotal 1.5Mdrwxrwx---. 1 root vboxsf 4.0K Apr 28 17:32 ./drwxrwx---. 1 root vboxsf 4.0K Apr 28 17:22 ../drwxrwx---. 1 root vboxsf    0 Apr 28 08:37 demo/-rwxrwx---. 1 root vboxsf 1.5M Apr 28 17:32 main*-rwxrwx---. 1 root vboxsf   58 Apr 28 08:37 main.go*$ ./maincall demo ...

以动态库方式编译时,如果删除动态库libdemo.so或者动态库libstd.so,运行main都会由于找不到动态库导致出错,例如删除动态库libdemo.so:

$ rm ../pkg/linux_amd64_dynlink/libdemo.so$ ./main./main: error while loading shared libraries: libdemo.so: cannot open shared object file: No such file or directory

以上就是go语言动态库的编译和使用方法,需要注意的是,其他go程序在使用go动态库时,必须提供动态库的源码,否则会编译失败。例如,这里将demo.go代码删除,再以动态库方式编译main.go时,会编译失败:

$ go install  -buildmode=shared -linkshared demo$ rm demo/demo.go$ go build -linkshared main.gomain.go:3:8: no buildable Go source files in /media/sf_share/git/go_practice/src/demo

动态库编译方式和静态库不一样,静态库可以不提供源码,直接使用静态库编译,而动态库不行。

相关文章:

go语言静态库的编译和使用


转载出处:
http://reborncodinglife.com/2018/04/29/how-to-create-dynamic-lib-in-golang/

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Golang1.7动态库的使用,C和Golang的动态库以及使用
使用scons替代makefile(2)
用 Cython 包装静态库和动态库
GoBypass - Golang免杀生成工具
Linux下GCC编程四个过程
如何用gcc编译生成动态链接库*.so文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服