golang全静态编译

那尔撒斯 894 2023-04-20

平时构建不依赖C runtime的代码都是用的

CGO_ENABLED=0 go build

这回因为有用到c的库,不得不打开CGO,上面这招就不能用了,直接执行会报错

root@racknerd-9cbc5e:~# ./main 
./main: /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by ./main)
./main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found (required by ./main)
./main: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found (required by ./main)

花了点时间,终于找到个全静态编译的办法,https://zhuanlan.zhihu.com/p/338891206

go build -ldflags '--extldflags "-static -fpic"' examples/main.go 

传到服务器上运行,

root@racknerd-9cbc5e:~# ./main 
error loading model: failed to open ./xxxx: No such file or directory
llama_init_from_file: failed to load model
Loading the model failed: failed loading model

效果拔群!