以太坊(go-ethereum)編譯調試環境搭建

原文http://blog.csdn.net/itleaks

以下步驟都是在MacOs上操作的,但同樣適合Ubuntu,只是有幾個小點有所不同,我會標註出來

 

編譯

1. Go環境搭建

    Mac: (brew是一個類似ubuntu apt-get的工具,用來在終端安裝軟件的)

brew update && brew upgrade

brew install git

brew install mercurial

brew install go

    Ubuntu:

sudo apt-get install git

sudo apt-get install golang-go

    Go代碼的package很多都是git項目,所以上面安裝git

    驗證go

go

    如果提示命令找不到就需要配置PATH

vim ~/.profile

    加入如下配置並重新登錄以讓配置生效

export GOROOT=/usr/local/go

export PATH=$GOROOT/bin:$PATH

2. go-ethereum下載及編譯

 git clone https://github.com/ethereum/go-ethereum.git

 make geth

 

編譯完成你會在build/bin/下找到二進制執行文件geth

    

調試

下載visual studio code

    Visual Studio Code是一個很強悍的閱讀調試代碼的IDE,強力推薦

https://code.visualstudio.com/Download

    這個網頁有mac版和ubuntu版本,選擇相應的平臺下載

設置GOPATH

     這裏需要注意,GOPATH不能設置爲go-ethereum目錄,而是go-ethereum/build/_workspace,爲啥是這個目錄,查看build/env.sh就知道了

#!/bin/sh

 

set -e

 

if [ ! -f "build/env.sh" ]; then

    echo "$0 must be run from the root of the repository."

    exit 2

fi

 

# Create fake Go workspace if it doesn't exist yet.

workspace="$PWD/build/_workspace"

root="$PWD"

ethdir="$workspace/src/github.com/ethereum"

if [ ! -L "$ethdir/go-ethereum" ]; then

    mkdir -p "$ethdir"

    cd "$ethdir"

    ln -s ../../../../../. go-ethereum

    cd "$root"

fi

 

# Set up the environment to use the workspace.

GOPATH="$workspace"

export GOPATH

 

# Run the command inside the workspace.

cd "$ethdir/go-ethereum"

PWD="$ethdir/go-ethereum"

 

# Launch the arguments with the configured environment.

exec "$@"

    也就是說編譯的時候GOPATH被臨時切換到go-ethereum/build/_workspace目錄了。

其實,還可以通過依賴庫來驗證,編譯完成後查看go-ethereum/build/_workspace目錄是否有依賴庫

ItleaksdeMacBook-Pro:build itleaks$ pwd

/Users/itleaks/projects/go-ethereum/build/_workspace

ItleaksdeMacBook-Pro:build itleaks$ tree

.

├── bin

│   ├── dlv

│   ├── go-outline

└── src

    ├── github.com

    │   ├── MichaelTJones

    │   │   └── walk

    │   │       ├── README.md

    │   │       ├── path_plan9.go

    │   │       ├── path_unix.go

    │   │       ├── path_windows.go

    │   │       ├── symlink.go

    │   │       ├── symlink_windows.go

    │   │       ├── walk.go

    │   │       └── walk_test.go

    │   ├── acroca

    │   │   └── go-symbols

    │   │       ├── LICENSE

    │   │       ├── README.md

    │   │       └── main.go

    │   ├── derekparker

    │   ├── ethereum

    │   │   └── go-ethereum -> ../../../../../.

    │   ├── golang

上面可以看出,確實存在。所以開始配置GOPATH

 

vim ~/.profile


添加

ItleaksdeMacBook-Pro:go-ethereum itleaks$ cat ~/.profile

export GOPATH=$HOME/projects/go-ethereum/build/_workspace

 

下載visual studio code go調試工具

 1)下載dlv

    有了這個工具才能配置go調試 

go get github.com/derekparker/delve/cmd/dlv

 

2)安裝GO插件

    安裝完成後點擊重新加載

 

3)導入代碼

4) 配置debug    

    然後就會生成launch.json

5)自動安裝其他調試依賴工具

    關閉,重新進入該項目

    這次會有以下提示,點擊install後就會安裝調試需要的工具

6)調試源碼

    進入cmd/geth/main.go添加斷點,必須進入這個文件,然後按F5即可進入調試

 

附錄:

同步主網:

geth --networkid 15 --cache=1024 --fast --rpc --rpcaddr 0.0.0.0 --rpcport 8545 --rpcapi personal,db,eth,net,web3,miner --nodiscover

 

--fast是指只同步區塊頭,不同步區塊,速度很快很多

/********************************

* 本文來自CSDN博主"愛踢門"

* 轉載請標明出處:http://blog.csdn.net/itleaks

******************************************/

發佈了11 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章