golang交叉编译CGO

记录在macOS上编译Windows和Linux的go程序,带CGO调用C代码,比如go-sqlite3包,已测试通过。

参考链接:

https://www.cnblogs.com/ibgo/p/6489488.html

http://www.mingw-w64.org/doku.php/download/macports

https://github.com/mattn/go-sqlite3/issues/532

https://github.com/mattn/go-sqlite3/issues/372#issuecomment-396863368

https://blog.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/

1. Windows平台(mingw-w64)

> 安装编译器:brew install mingw-w64

> 编译指令:CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -v

2. Linux平台(x86_64-linux-musl-gcc)

> 安装编译器:brew install FiloSottile/musl-cross/musl-cross

>编译指令:CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CGO_LDFLAGS="-static" go build -a -v

注:

-a:重新编译

-static表示静态连接,没有这个选项,linux上运行报:-bash: ./xxx: /lib/ld-musl-x86_64.so.1: bad ELF interpreter: No such file or directory

你可能感兴趣的:(golang)