源码的编译-Why?
Fabric源码的编译是建立在已经基本掌握了Fabric的部分模块或者很多模块的代码的基础上.但是为什么要编译Fabric的源码呢,大神们不是已经写好了Fabric这个项目框架并编译好了吗,拿来用不就好了吗?
1.部署Fabric网络的docker容器是通过编译Fabric里的peer/Order/fabric-ca/模块的代码形docker镜像创建的.固有的已编译好的镜像让我们要开发的系统也禁锢住了,每个要开发的应用系统都有自身的特点,我们需要通过编译Fabric的改写的模块代码生成应用系统所需要的镜像.比如说,阿里通过基于OpenJDK定制的TaobaoVM,也是自身编译的软件开发包.
2.编译Fabric源码可以验证自己的想法,当阅读Fabric源码时候,代码如何的运行只是通过我们理论上的猜想,通过实际的编译代码可以验证我们的猜想.
3.定位错误,修改bug.源码的编译是建立在Fabric的源码的阅读上的,当系统运行的日志里出现错误时,根据错误提示能定位到源码中的错误点,根据源码能修改bug.
准备工作
export GOPATH=/home/gopathgo get -u github.com/gpmgo/gopm/home/gopath/bin目录下将有gopm,go get是下载和编译的意思export GOPATH=/home/gopathgopm get -g -d golang.org/x/tools/cmd/goimports再使用 go install 安装 goimportsgo install golang.org/x/tools/cmd/goimports/home/gopath/bin目录下将有goimportsGOPATH=/home/gopathgopm get -g -d golang.org/x/tools/covergopm get -g -d github.com/axw/gocov/gocovgo install github.com/axw/gocov/gocov/home/gopath/bin目录下将有gocovGOPATH=/home/gopathgopm get -g -d github.com/AlekSi/gocov-xmlgo install github.com/AlekSi/gocov-xml/home/gopath/bin目录下将有 gocov-xmlyum install -y gcc libtool libltdl-dev libtool-ltdl-devel openssl
伪目标eg: .PHONY lable.PHONY避免同名 lable: rm *.o一般形式的eg : main.o : main.c defs.h cc -c main.c #make最后生成 main.o文件依赖于main.c defs.h两个文件,cc -c main.c执行可执行文件main.c进行编译.
How
export GOPATH= /home/gopathsudo sucd $GOPATHgopm get -g -d github.com/golang/protobuf/protoc-gen-gogo install github.com/golang/protobuf/protoc-gen-go/home/gopath/bin出现protoc-gen-go执行文件make peer/all/orderer...

执行make命令后会在当前文件夹下生成一个build文件夹.编译后的镜像或者可运行工具都是在这个文件夹里找.
编译peer模块的代码示例
peer node start
package test
import (
"github.com/hyperledger/fabric/common/flogging"
"github.com/spf13/cobra"
)
var logger = flogging.MustGetLogger("testCommand")
func Cmd() *cobra.Command {
return testCommond
}
var testCommond = &cobra.Command{
Use: "test",
Short: "test the node.",
Long: `test a node that interacts with the network.`,
Run: func(cmd *cobra.Command, args []string) {
logger.Info("Build test by JinaWenJun----------------------")
},
}
2.在/home/gopath/src/hyperledger/fabric/peer下的main.go文件中添加
mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(node.Cmd())
mainCmd.AddCommand(chaincode.Cmd(nil))
mainCmd.AddCommand(clilogging.Cmd(nil))
mainCmd.AddCommand(channel.Cmd(nil))
//添加的另外一个test命令,并在前面导入包"github.com/hyperledger/fabric/peer/test"
mainCmd.AddCommand(test.Cmd())
make peerpeer test

遇到的问题解决方案
1.终端执行make 命令时,出现没有那个文件或目录如图,

方法:需要在终端下添加环境变量,执行
export GOPATH= /home/gopath
2.当因为网速终端卡住时,需要将Makefile文件中的curl部分删除.手动下载相应的文件chaintool目录/build/bin下面.

3.当出现文件找不到或者命令找不到,如图

方法:终端执行下面两条命令,将命令复制进去
cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/
cp $GOPATH/bin/gocov $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin/
4.当终端make peer出现下图错误时候.

方法:根据错误提示
core/chaincode/ccproviderimpl.go:20:2:
不能找到context.进入到相应的文件ccproviderimpl.go修改.
如果在出现提示哪里不能找到"context"到指定的文件下按照上述形式修改即可.
编译成功:

有疑问加站长微信联系(非本文作者)
