当前位置:网站首页>[go language] detailed explanation of dynamic library and static library
[go language] detailed explanation of dynamic library and static library
2022-07-19 01:51:00 【tissar】
【Go Language 】 Detailed explanation of dynamic library and static library
Preface
First , Quote seven cattle cloud storage team in 《Go Language programming 》 Point of view ( The first 7 Chapter , The first 9 section ):
For now , Distribute in binary Go Bag is not very realistic . because Go The language has very strict compatibility control , Any different version number will result in the failure to link packages . therefore , If you use Go Language developed a library , Then the most appropriate way to distribute the library is to package the source code package directly and distribute it , Compiled by the user .
however , The author thinks Go Language compilation and C The language is the same , With compilation 、 Link phase . therefore , It is necessary to understand 、 Study Go Dynamic library and static library of language , It also helps distribute packages later .
Example project
A standard Golang The project includes 3 A catalog
- bin Put the executable
- pkg Put the static library
- src Put the source code
[[email protected] goprj]$ ls
bin pkg src
[[email protected] goprj]$ tree
.
├── bin
├── pkg
│ ├── gccgo_linux_amd64
│ └── linux_amd64
└── src
├── calc
│ ├── fibonacci
│ │ ├── fibonacci.go
│ │ └── fibonacci_test.go
│ └── calc.go
└── simplemath
├── add.go
└── sqrt.go
6 directories, 5 files
[[email protected] goprj]$
pkg
Wait, I use google Of gc Tool chain , Also used gnu Of gccgo Tool chain , therefore pkg There are two folders below
- gccgo_linux_amd64 discharge gccgo Generated static library
- linux_amd64 discharge gc Generated static library
src
The code has 2 Packages and 1 Executable files
- calc The main program
- calc/fibonacci Provide Fibonacci number correlation function
- simplemath Provide basic mathematical operations
src/calc/calc.go
package main
import "fmt"
import "calc/fibonacci"
func main() {
var res int64
var err error
res, err = fibonacci.Fibonacci(30)
if err != nil {
panic(err.Error())
} else {
fmt.Println("Result:", res)
}
res, err = fibonacci.Fibonacci_r(30)
if err != nil {
panic(err.Error())
} else {
fmt.Println("Result:", res)
}
}
src/calc/calc/fibonacci/fibonacci.go
package fibonacci
import "simplemath"
import "errors"
func Fibonacci(n int64) (int64, error) {
if n < 1 {
err := errors.New("Should be greater than 0!")
return 0, err
} else if n > 92 {
err := errors.New("Should be less than 93!")
return 0, err
}
var res int64 = 0
var tmp int64 = 1
var idx int64 = 0
for ; idx < n; idx++ {
res = simplemath.Add(res, tmp)
res, tmp = tmp, res
}
return res, nil
}
func Fibonacci_r(n int64) (int64, error) {
if n < 1 {
err := errors.New("Should be greater than 0!")
return 0, err
} else if n < 3 {
return 1, nil
} else if n > 92 {
err := errors.New("Should be less than 93!")
return 0, err
}
lhs, _ := Fibonacci_r(n - 1)
rhs, _ := Fibonacci_r(n - 2)
ret := simplemath.Add(lhs, rhs)
return ret, nil
}
src/simplemath/add.go
package simplemath
func Add(a int, b int) int {
return a + b
}
src/simplemath/sqrt.go
package simplemath
import "math"
func Sqrt(i int) int {
v := math.Sqrt(float64(i))
return int(v)
}
One button compilation
First , Add the project path to the environment variable .
then , use go install Command one click compilation
- gc
go install ./... - gccgo
go install -compiler gccgo ./...
gc Compiling static libraries
- use
go tool compileCompile binary files - use
go tool packPackage as a static library
matters needing attention :
- The same package should be compiled into the same
.ofile - gc The naming rule of static libraries is
gopackage.a - You need to use this static library in the future -I and -L Parameters
[[email protected] goprj]$ go tool compile -o simplemath.o src/simplemath/add.go src/simplemath/sqrt.go
[[email protected] goprj]$ go tool pack c pkg/linux_amd64/simplemath.a simplemath.o
[[email protected] goprj]$
[[email protected] goprj]$ go tool compile -o fibonacci.o -I pkg/linux_amd64 src/calc/fibonacci/fibonacci.go
[[email protected] goprj]$ go tool pack c pkg/linux_amd64/calc/fibonacci.a fibonacci.o
[[email protected] goprj]$
[[email protected] goprj]$ go tool compile -o calc.o -I pkg/linux_amd64 src/calc/calc.go
[[email protected] goprj]$ go tool link -o bin/calc -L pkg/linux_amd64 calc.o
[[email protected] goprj]$
gccgo Compiling static libraries
- use
gccgoCompile binary files - use
arPackage as a static library
matters needing attention :
- The same package should be compiled into the same
.ofile - gccgo The naming rule of static libraries is
libgopackage.a
[[email protected] goprj]$ gccgo -c -o simplemath.o src/simplemath/add.go src/simplemath/sqrt.go
[[email protected] goprj]$ ar rv pkg/gccgo_linux_amd64/libsimplemath.a simplemath.o
ar: creating pkg/gccgo_linux_amd64/libsimplemath.a
a - simplemath.o
[[email protected] goprj]$
[[email protected] goprj]$ gccgo -c -o fibonacci.o -I pkg/gccgo_linux_amd64 src/calc/fibonacci/fibonacci.go
[[email protected] goprj]$ ar rv pkg/gccgo_linux_amd64/calc/libfibonacci.a fibonacci.o
ar: creating pkg/gccgo_linux_amd64/calc/libfibonacci.a
a - fibonacci.o
[[email protected] goprj]$
[[email protected] goprj]$ gccgo -c -o calc.o -I pkg/gccgo_linux_amd64 src/calc/calc.go
[[email protected] goprj]$ gccgo -o bin/calc \
> -L pkg/gccgo_linux_amd64 \
> -L pkg/gccgo_linux_amd64/calc \
> calc.o \
> -lfibonacci -lsimplemath
[[email protected] goprj]$
gccgo Compile dynamic library
gcc Have rich compilation experience , Compilation is also provided go Tools for dynamic libraries .
The easiest way is to use -shared Compilation options
[[email protected] goprj]$ gccgo -shared -fPIC \
> -o pkg/gccgo_linux_amd64/libsimplemath.so \
> src/simplemath/add.go \
> src/simplemath/sqrt.go
[[email protected] goprj]$ gccgo -shared -fPIC \
> -o pkg/gccgo_linux_amd64/calc/libfibonacci.so \
> -I pkg/gccgo_linux_amd64 \
> src/calc/fibonacci/fibonacci.go
[[email protected] goprj]$
summary
in general ,go tool Very convenient , Manual compilation is more flexible .
边栏推荐
- C language operator priority
- Nmap and Nikto scanning
- README.md添加目录
- bais mintigation post-processing for individual and group fairness
- Why do you spend 1.16 million to buy an NFT avatar in the library of NFT digital collections? The answer may be found by reviewing the "rise history" of NFT avatars
- Red sun range 2
- js 树状图数组批量循环操作
- VSCode中安装Go:tools failed to install.
- 网络安全新架构:零信任安全
- iFair: Learning Individually Fair Data Representations for Algorithmic Decision Making
猜你喜欢

Solve the problem that Scala cannot initialize the class of native

基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享

apt-get update报错:Hash 校验和不符

温州大学X袋鼠云:高等人才教育建设,如何做到“心中有数”

字节二面:什么是伪共享?如何避免?

Dhfs read / write process

MapReduce environment preparation

【MySQL】windows安装MySQL 5.7

【文献阅读】Small-Footprint Keyword Spotting with Multi-Scale Temporal Convolution

1章 性能平台GodEye源码分析-整体架构
随机推荐
基于机器学习技术的无线小区负载均衡自优化
[踩坑]packets.go:428: busy buffer invalid connection
binary search
rotoc-gen-go: unable to determine Go import path for **.proto
手把手带你从零开始完整开发经典游戏【俄罗斯方块】,全部逻辑只用不到200行代码。
【文献阅读】TENET: A Framework for Modeling Tensor Dataflow Based on Relation-centric Notation
golang编译的常见错误
使用异或交换两个变量是低效的
Learning Transferable Visual Models From Natural Language Supervision
Namenode and secondarynamenode
Today's code farmer girl did exercises on breathing lights, controlled components and high-level components
NFT differentiation trend has shown, how to capture value?
IPFs file persistence operation
面试官问:Redis 突然变慢了如何排查?
Redis+Caffeine两级缓存,让访问速度纵享丝滑
js 树状图数组批量循环操作
Redis suddenly slowed down?
AVPlayer添加播放进度监听
每日10道面试题打卡——JVM篇
【文献阅读】Counting Integer Points in Parametric Polytopes Using Barvinok‘s Rational Functions