linux下编译go文件,Linux系统编译安装Go1.9.2 - Go语言中文社区

linux下编译go文件,Linux系统编译安装Go1.9.2


wget https://storage.googleapis.com/golang/go.src.tar.gz

tar -zxvf go.src.tar.gz

sudo mv go /user/local/

默认放到/user/local/目录下,然后进行安装

cd /usr/local/go/src

./all.bash

执行之后发现报错

vagrant@homestead:/usr/local/go/src$ ./all.bash

##### Building Go bootstrap tool.

cmd/dist

ERROR: Cannot find /home/vagrant/go1.4/bin/go.

Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.

为什么会报错找不到go1.4呢?不是还没安装呢吗?原来编译GO 1.6版本以上的需要依赖GO 1.4版本的二进制

于是要先编译安装1.4版本

首先要确定是否安装了 gcc和glibc-devel

$ cd ~/

$ git clone git@github.com:golang/go.git

$ cd go/

$ git checkout -b 1.4 go1.4 //将远程的go1.4分支checkout

$ cd ~/go/src

$ sudo ./all.bash

$ cp -rf ~/go/ ~/go1.4

编译过程中遇到了报错

# cmd/pprof

/home/vagrant/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?

/home/vagrant/go/pkg/linux_amd64/runtime/cgo.a(_all.o): unknown relocation type 42; compiled without -fpic?

runtime/cgo(.text): unexpected relocation type 298

runtime/cgo(.text): unexpected relocation type 298

解决方案:

$ cd ~/go1.4/src

$ sudo CGO_ENABLED=0 ./make.bash

导致上述问题的原因是:CGO_ENABLED: Controls cgo usage during the build. Set it to 1 to include all cgo related files, .c and .go file with “cgo” build directive, in the build. Set it to 0 to ignore them.在构建过程中控制cgo的使用。当设置为1,在构建时,会包含所有cgo相关的文件,如带有”cgo”编译指令的.c和.go文件。当设置为0,则忽略它们(即禁用CGO)

编译完了go1.4版本,然后就可以编译v1.9.2了

$ cd ~/go

$ git clean -dfx //删除当前目录下所有没有track过的文件. 不管他是否是.gitignore文件里面指定的文件夹和文件

$ git checkout -b 1.9.2 go1.9.2

$ cd ~/go/src

$ sudo ./all.bash

配置环境变量

在 ~/.profile文件末尾追加如下命令,在打开一个回话的时候,会自动执行~/.profile,配置环境变量

export GOROOT=/usr/local/go

export PATH=$PATH:$GOROOT/bin

export GOPATH=/usr/local/go/production

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_30999777/article/details/116558459
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-03 14:35:13
  • 阅读 ( 261 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢