1. 程式人生 > 其它 >Golang原始碼分析-建立開發除錯環境

Golang原始碼分析-建立開發除錯環境

技術標籤:Golang學習go

建立開發除錯環境

建立開發環境

安裝主機環境:18.04.1-Ubuntu

安裝輸入法

安裝玩Ubuntu後機器預設不支援中文輸入法,需要先安裝中文輸入法。具體安裝過程參考Ubuntu 18.04搜狗輸入法安裝

更新軟體源

系統預設源速度太慢,可以更新為國內的源,此處使用網易的源,具體方法如下:
sudo vi /etc/apt/sources.list

#163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

最後

sudo apt update
sudo apt upgrade
sudo apt install vim

安裝gcc

系統預設安裝的gcc版本比較新,gcc1.4版本程式碼有報錯,因此此處安裝稍微舊一些的版本,此處我安裝的是gcc-5
安裝gcc命令:sudo apt-get install -y gcc-5

Go環境編譯

GO1.5實自舉(用GO實現GO的編譯器),因此保留兩個版本的Go環境。

  • Go1.4是用C語言實現
  • GO1.5是第一個Go語言實現自舉的版本

C版本Go1.4

在https://studygolang.com/dl下載原始碼包go1.4-bootstrap-20171003.tar.gz 。解壓後進入src目錄執行

./make.bash 

之後出現報錯
/usr/bin/ld: -r and -pie may not be used together

# runtime/cgo
/usr/bin/ld: -r and -pie may not be used together
collect2: error: ld returned 1 exit status

最後回到bin目錄下,執行一下bin目錄下的go程式如果如下圖所示就編譯成功了。
在這裡插入圖片描述

Go版本Go1.5

編譯Go1.5和編譯Go1.4類似,將下載的Go1.5程式碼包解壓後進入src目錄,執行以下命令

 GOROOT_BOOTSTRAP=/home/zzl/golang/go1.4 ./make.bash

注意以上命令不可以分成兩條,否則會報下圖的錯誤。
在這裡插入圖片描述最後回到go1.5目錄下的bin子目錄執行一下go命令檢查編譯是否成功:
在這裡插入圖片描述