比特币BTC源码分析(0):环境搭建

一、Bitcoin编译运行启动过程

1、从Github上clone bitcoin源码 至本地

~/go/src/github.com/bitcoin$git clone https://github.com/bitcoin/bitcoin.git
Cloning into 'bitcoin'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 126886 (delta 7), reused 3 (delta 3), pack-reused 126870
Receiving objects: 100% (126886/126886), 113.75 MiB | 6.86 MiB/s, done.
Resolving deltas: 100% (88524/88524), done.
~/go/src/github.com/bitcoin

2、根据官方文档指导进行编译安装

各系统版本安装方法:
Mac -osx
Linux
Window

主要命令:

~/go/src/github.com/bitcoin/bitcoin$./autogen.sh
#开启debug进行configure配置
~/go/src/github.com/bitcoin/bitcoin$./configure -with-gui --enable-debug 
#成功配置结果如下
Options used to compile and link:
  with wallet   = yes
  with gui / qt = yes
  with bip70  = yes
  with qr     = yes
  with zmq      = no
  with test     = yes
  with bench    = yes
  with upnp     = yes
  use asm       = yes
  sanitizers    =
  debug enabled = yes
  gprof enabled = no
  werror        = no
  target os     = darwin
  build os      = darwin
  CC            = gcc
  CFLAGS        = -g -O2
  CPPFLAGS      =  -DDEBUG -DDEBUG_LOCKORDER  -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2  -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS -I/usr/local/opt/berkeley-db@4/include -DMAC_OSX
  CXX           = g++ -std=c++11
  CXXFLAGS      =  -Og -g3 -ftrapv  -Wstack-protector -fstack-protector-all  -Wall -Wextra -Wformat -Wvla -Wformat-security -Wthread-safety-analysis -Wrange-loop-analysis -Wredundant-decls  -Wno-unused-parameter -Wno-self-assign -Wno-unused-local-typedef -Wno-deprecated-register -Wno-implicit-fallthrough
  LDFLAGS       = -pthread    -Wl,-headerpad_max_install_names -Wl,-dead_strip
  ARFLAGS       = cr

~/go/src/github.com/bitcoin/bitcoin$make check

3、编译结果目录结构

(1)./src/为整个比特币核心的代码,由C++编写;

(2)./src/bitcoind为比特币核心启动程序;

(3)./src/bitcoin-cli为客户端控制程序,命令行中会用到;

(4)./qt/为qt项目目录,qt的客户端其实就是调用了bitcoind和bitcoin-cli的接口。

4、安装目录(包含输出日志、区块链等等)

默认在其他目录下:
Linux在~/.bitcoin中;
Mac在/User/YOURNAME/Library/Application Support/Bitcoin中,不太好找,用前往或者终端;
windows在C盘下的Bitcoin。
如果想在代码里改改默认路径可以去./src/utils.h第455行左右,找GetDefaultDataDir函数。
可用命令行跟踪日志文件,日志文件在上面说的bitcoin文件夹里面(mac上直接点开文件就行)

5、命令行启动

(1)命令行启动客户端

~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind 
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind -daemon #后台启动,一般都要加,然后在debug.log里看输出就行
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind -gen=1 #自动挖矿
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoind help #其他的自己看去吧

(2)操作客户端

~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli stop
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli getinfo #查看当前信息
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli getpeerinfo #查看其他节点
~/go/src/github.com/bitcoin/bitcoin$./src/bitcoin-cli help #其他的自己看去吧

你可能感兴趣的:(比特币BTC源码分析(0):环境搭建)