在实际开发中,我们往往要使用交叉编译[1]来实现跨平台部署,这两天查阅资料,发现之所以TensorFlow跨平台编译失败,是因为使用了cgo来调用TensorFlow C。今天我们就来深入了解下,怎么处理带CGO的交叉编译。
在没有CGO调用的情况下,交叉编译只需带上三个参数便可以实现
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
或者加上可选参数
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags ‘-s -w --extldflags “-static -fpic”’ main.go
CGO_ENABLED 这个参数默认为1,开启CGO。需要指定为0来关闭,因为CGO不支持交叉编译。
GOOS 和 GOARCH 用来指定要构建的平台为Linux
可选参数-ldflags 是编译选项:
-s -w 去掉调试信息,可以减小构建后文件体积,
–extldflags “-static -fpic” 完全静态编译[2],这样编译生成的文件就可以任意放到指定平台下运行,而不需要运行环境配置。
显然对于带CGO的交叉编译,CGO_ENABLED必须开启。这也就需要辅助编译器来帮我们实现交叉编译了。
如果你是mac平台,可以用这个工具 FiloSottile/musl-cross/musl-cross 直接通过brew安装就可以使用
brew install FiloSottile/musl-cross/musl-cross
安装成功后,有多种编译器可以帮我们实现交叉编译,使用时只需在编译对应参数下指定就可以了。
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
通过CC=x86_64-linux-musl-gcc 来指定GCC编译器。而CGO_LDFLAGS="-static"来指定CGO部分的编译为静态编译。
这样就可以实现带CGO的交叉编译啦。如果你是其他平台,也可以通过跨平台编译的工具musl 下载对应平台工具,这里有支持多平台实现工具。下载解压,将解压好的目录下 bin 文件路径,放到PATH环境变量中就可以啦。
brew install FiloSottile/musl-cross/musl-cross
安装过程中出现如下问题:
Error: An exception occurred within a child process: NoMethodError: undefined method `path’ for nil:NilClass Did you mean? paths
解决方案
使用命令brew update-reset升级Homebrew即可[1]
References
[1] https://discourse.brew.sh/t/homebrew-installation-fail/7439
再次安装brew install FiloSottile/musl-cross/musl-cross
提示如下:
lizhongsu@lizhongdeMBP ~ % brew install FiloSottile/musl-cross/musl-cross
==> Downloading https://ghcr.io/v2/homebrew/portable-ruby/portable-ruby/blobs/sha256:b065e5e3783954f3e65d8d3a6377ca51649bfcfa21b356b0dd70490f74c6bd86
##################################################################################################################################################################################################### 100.0%
==> Pouring portable-ruby-2.6.3_2.yosemite.bottle.tar.gz
Error:
homebrew-core is a shallow clone.
To `brew update`, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub's request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don't do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
==> Installing musl-cross from filosottile/musl-cross
==> Downloading https://f001.backblazeb2.com/file/filippo-public/musl-cross-0.9.9_1.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring musl-cross-0.9.9_1.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/musl-cross/0.9.9_1: 1,851 files, 246.3MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/lizhongsu/Library/Caches/Homebrew/autoconf--2.69.catalina.bottle.4.tar.gz... (871.6KB)
Removing: /Users/lizhongsu/Library/Caches/Homebrew/automake--1.16.2.catalina.bottle.tar.gz... (948.7KB)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/autoconf... (64B)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/automake... (64B)
Removing: /Users/lizhongsu/Library/Logs/Homebrew/telnet... (64B)
Pruned 2 symbolic links from /usr/local
有一个错误提示:
Error:
homebrew-core is a shallow clone.
To brew update
, first run:
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch --unshallow
This command may take a few minutes to run due to the large size of the repository.
This restriction has been made on GitHub’s request because updating shallow
clones is an extremely expensive operation due to the tree layout and traffic of
Homebrew/homebrew-core and Homebrew/homebrew-cask. We don’t do this for you
automatically to avoid repeatedly performing an expensive unshallow operation in
CI systems (which should instead be fixed to not use shallow clones). Sorry for
the inconvenience!
暂时没有进行根据提示进行处理,尝试编译,成功!
执行命令
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CGO_LDFLAGS="-static" go build -a -v
成功交叉编译完成Go在linux下可运行文件。
total 149968
8 -rw-rw-r--@ 1 lizhongsu staff 697 Oct 10 2020 Dockerfile
98320 -rw-------@ 1 lizhongsu staff 50152502 Mar 1 16:53 EasyRTC.zip
8 -rw-rw-r--@ 1 lizhongsu staff 2244 Oct 10 2020 README.md
51416 -rwxr-xr-x 1 lizhongsu staff 25711520 Apr 17 03:53 adminserver
0 drwxrwxr-x@ 5 lizhongsu staff 160 Oct 10 2020 app
8 -rw-rw-r--@ 1 lizhongsu staff 264 Apr 15 15:10 config.toml
0 drwxrwxr-x@ 9 lizhongsu staff 288 Oct 10 2020 db
0 drwxrwxr-x@ 5 lizhongsu staff 160 Jan 12 17:24 docs
96 -rw-r--r-- 1 lizhongsu staff 49152 Feb 24 16:40 easyrtc.db
8 -rw-rw-r--@ 1 lizhongsu staff 871 Oct 10 2020 go.mod
88 -rw-rw-r--@ 1 lizhongsu staff 42294 Oct 10 2020 go.sum
8 -rw-rw-r--@ 1 lizhongsu staff 846 Oct 10 2020 main.go
8 -rw-rw-r--@ 1 lizhongsu staff 357 Oct 10 2020 package.json
0 drwxrwxr-x@ 5 lizhongsu staff 160 Oct 10 2020 routes
0 drwxrwxr-x@ 11 lizhongsu staff 352 Jan 12 18:27 server
0 drwxrwxr-x@ 4 lizhongsu staff 128 Oct 10 2020 util
0 drwxrwxr-x@ 30 lizhongsu staff 960 Apr 15 15:58 web_src
其中 adminiserver 就是生成文件(linux)
2021年04月17日
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)