本帖只谈技术实现,首先我们自己来编译一个比特币客户端吧。
参考github上的描述文件
https://github.com/bitcoin/bitcoin/blob/master/doc/build-msw.md
1) 签出代码
GitHub上的官方源代码
git clone https://github.com/bitcoin/bitcoin.git
也可以从csdn的code上签出本文的整个项目组
git clone git://code.csdn.net/wuzh1230/bitcoin.git
提示:
<1>已经包含了预编译的依赖库在3rd目录下;
<2>已经包含了ming32工具链,请修改msys/1.0/etc/fstab。
2) 下载依赖包
Libraries you need to download separately and build:
name default path download
----------------------------------------------------
OpenSSL \openssl-1.0.1c-mgw http://www.openssl.org/source/
Berkeley DB \db-4.8.30.NC-mgw http://www.oracle.com/technology/software/products/berkeley-db/index.html
Boost \boost-1.50.0-mgw http://www.boost.org/users/download/
miniupnpc \miniupnpc-1.6-mgw http://miniupnp.tuxfamily.org/files/
3) 编译依赖包
3.1) OpenSSL
MSYS shell:
un-tar sources with MSYS 'tar xfz' to avoid issue with symlinks (OpenSSL ticket 2377) change 'MAKE' env. variable from 'C:\MinGW32\bin\mingw32-make.exe' to '/c/MinGW32/bin/mingw32-make.exe'
cd /c/openssl-1.0.1c-mgw
./config
make
使用最新的mingw来构建未发现问题。
3.2) Berkeley DB
MSYS shell:
cd /c/db-4.8.30.NC-mgw/build_unix
sh ../dist/configure --enable-mingw --enable-cxx
make
注意:卸载mingw安装包中pthread组件,否则,BDB会尝试构建repmgr模块(repmgr_posix.c需要posix支持,mingw32不支持)。
3.3)
Boost
MSYS shell:
downloaded boost jam 3.1.18
cd \boost-1.50.0-mgw
bjam toolset=gcc --build-type=complete stage
(1) 准备bjam.exe,考虑到boost对mingw的支持不是很好,折中的办法是用vc++的命令行构建一个bjam(比如:bootstrap.bat vc9);然后用这个b2.exe构建mingw版本的boost。
(2)执行 b2 --prefix=/d/workspace/temp install toolset=gcc link=shared variant=release runtime-link=shared threading=multi --without-mpi --without-python --without-wave --without-graph --without-graph_parallel
3.4)
MiniUPnPc
UPnP support is optional, make with USE_UPNP=
to
disable it.
MSYS shell:
cd /c/miniupnpc-1.6-mgw
make -f Makefile.mingw
mkdir miniupnpc
cp *.h miniupnpc/
(1)修改一个miniupnpc.def里面的LIBRARY指定的库名称为空的问题;
(2)修改一个Makefile.mingw的del命令;
4) 注意事项
4.1) boost
Ticket #8725 (closed
Bugs: fixed)
2) MINGW构建boost失败
boost官方网站表示cygwin和mingw的支持程度是不一样的
3) 导出common_config_file_iterator
https://github.com/boostorg/program_options/commit/4ae33ce15e5b6345e6eefa19466fdf7cd28a7bbd
4.2) MiniUPNPc
1) 修改Makefile.mingw, 添加del定义
# $Id: Makefile.mingw,v 1.16 2011/04/18 17:39:31 nanard Exp $ # Miniupnp project. # http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ # (c) 2005-2011 Thomas Bernard # This Makefile is made for MinGW # CC = gcc #CFLAGS = -Wall -g -DDEBUG -D_WIN32_WINNT=0X501 CFLAGS = -Wall -Os -DNDEBUG -D_WIN32_WINNT=0X501 LDLIBS = -lws2_32 -liphlpapi DEL = rm -rf
2) 封装dll的时候出错:
gcc -enable-stdcall-fixup -o upnpc-static upnpc.o libminiupnpc.a -lws2_32 -liphl papi d:/cifs/mingw32/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: war ning: cannot find entry symbol nable-stdcall-fixup; defaulting to 00000000004010 00 dllwrap -k --driver-name gcc \ --def miniupnpc.def \ --output-def miniupnpc.dll.def \ --implib miniupnpc.lib -o miniupnpc.dll \ dll/miniwget.o dll/minixml.o dll/igd_desc_parse.o dll/minisoap.o dll/min iupnpc.o dll/upnpreplyparse.o dll/upnpcommands.o dll/upnperrors.o dll/connecthos tport.o dll/portlistingparse.o dll/receivedata.o -lws2_32 -liphlpapi D:\cifs\mingw32\bin\dlltool: Syntax error in def file miniupnpc.def:5 D:\cifs\mingw32\bin\dlltool: Syntax error in def file miniupnpc.def:5
看看def文件的第5行,dlltool工具不允许空的library指令,修改一下,添加一下库的名字:
LIBRARY ; miniupnpc library miniupnpc
4.3) BerkeleyDB
使用最新的mingw(不带pthread组件)编译Berkeley DB.
分析BDB的configure.ac里面的这个宏,它会检测pthread可用性,如果可用,尝试包含REPMGR_OBJS
# Replication can be disabled. if test "$db_cv_build_replication" = "yes"; then AC_DEFINE(HAVE_REPLICATION) AH_TEMPLATE(HAVE_REPLICATION, [Define to 1 if building replication support.]) ADDITIONAL_OBJS="$ADDITIONAL_OBJS \$(REP_OBJS)" # If we're building replication and detected POSIX threads, build the # replication manager. AH_TEMPLATE(HAVE_REPLICATION_THREADS, [Define to 1 if building the Berkeley DB replication framework.]) if test "$ac_cv_header_pthread_h" = yes; then AC_DEFINE(HAVE_REPLICATION_THREADS) # Solaris requires the socket and nsl libraries to build the # replication manager. Don't add nsl regardless of the OS, # it causes RPC to fail on AIX 4.3.3. case "$host_os" in solaris*) AC_HAVE_LIBRARY(nsl, LIBSO_LIBS="$LIBSO_LIBS -lnsl") AC_HAVE_LIBRARY(socket, LIBSO_LIBS="$LIBSO_LIBS -lsocket");; esac ADDITIONAL_OBJS="$ADDITIONAL_OBJS \$(REPMGR_OBJS)" else ADDITIONAL_OBJS="$ADDITIONAL_OBJS repmgr_stub${o}" fi else ADDITIONAL_OBJS="$ADDITIONAL_OBJS rep_stub${o} repmgr_stub${o}" fi
5) 编译bitcoin客户端
编译脚本samplebuild.sh的代码,请修改DEPEND_HOME变量
export DEPEND_HOME=/d/cifs/try/buildbtc/csdn/bitcoin/3rd ./configure \ --disable-tests \ --with-boost="${DEPEND_HOME}/boost-1.50.0-mgw" \ --with-miniupnpc=yes \ CPPFLAGS="-D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -w -g -O0 -I${DEPEND_HOME}/db-4.8.30.NC-mgw/include -I${DEPEND_HOME}/miniupnpc-1.6-mgw/include -I${DEPEND_HOME}/openssl-1.0.1c-mgw/include -I${DEPEND_HOME}/boost-1.50.0-mgw/include/boost-1_50" \ LDFLAGS="-L${DEPEND_HOME}/db-4.8.30.NC-mgw/lib -L${DEPEND_HOME}/miniupnpc-1.6-mgw/lib -L${DEPEND_HOME}/openssl-1.0.1c-mgw/lib"
5.1) 解决::UnRegisterSleepEx()错误
添加 -D_WIN32_WINNT=0x0601 -DWINVER=0x0601
5.2) 解决WSAPOLLFD未定义的问题
typedef struct pollfd { SOCKET fd; short events; short revents; } WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;
5.3) 解决boost连接问题
1) boost_program_options
查看src/m4/ax_boost_program_options.m4的第74行附近:
for libextension in `ls $BOOSTLIBDIR/libboost_program_options*.so* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.so.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.dylib* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.dylib.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.a.*$;\1;'` ; do
修改为:
for libextension in `ls $BOOSTLIBDIR/libboost_program_options*.so* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.so.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.dylib* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.dylib.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.dll.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.dll\.a.*$;\1;'` ; do
2) boost_chrono
查看src/m4/ax_boost_chrono.m4的第81行附近:
for libextension in `ls $BOOSTLIBDIR/libboost_chrono*.so* $BOOSTLIBDIR/libboost_chrono*.dylib* $BOOSTLIBDIR/libboost_chrono*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_chrono.*\)\.so.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.a.*$;\1;'` ; do
修改为:
for libextension in `ls $BOOSTLIBDIR/libboost_chrono*.so* $BOOSTLIBDIR/libboost_chrono*.dylib* $BOOSTLIBDIR/libboost_chrono*.dll.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_chrono.*\)\.so.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.dylib.*$;\1;' -e 's;^lib\(boost_chrono.*\)\.dll\.a.*$;\1;'` ; do
5.4) 修复了一个bitcoin编译时候的库依赖顺序
参考:https://github.com/bitcoin/bitcoin/commit/d696820b45d9b6be1c21a65f2c8ed3b260938cb7
6) 成功构建bitcoind
sh autogen.sh && sh samplebuild.sh && make
$ make ...//很多输出 CXXLD bitcoind.exe CXX bitcoin-cli.o CXXLD bitcoin-cli.exe make[3]: Leaving directory `/d/cifs/try/buildbtc/csdn/bitcoin/bitcoin-master/src' make[2]: Leaving directory `/d/cifs/try/buildbtc/csdn/bitcoin/bitcoin-master/src' make[1]: Leaving directory `/d/cifs/try/buildbtc/csdn/bitcoin/bitcoin-master/src' make[1]: Entering directory `/d/cifs/try/buildbtc/csdn/bitcoin/bitcoin-master' make[1]: Nothing to be done for `all-am'. make[1]: Leaving directory `/d/cifs/try/buildbtc/csdn/bitcoin/bitcoin-master'
复制所有的依赖库dll到exe所在目录。
$ ls libboost_*-mgw48-mt-1_50.dll libcharset-1.dll libdb-4.8.dll libdb_cxx-4.8.dll libexpat-1.dll libgcc_s_dw2-1.dll libgettextlib-0-18-3.dll libgettextpo-0.dll libgettextsrc-0-18-3.dll libgmp-10.dll libgmpxx-4.dll libgomp-1.dll libiconv-2.dll libintl-8.dll libltdl-7.dll libmpc-3.dll libmpfr-4.dll libquadmath-0.dll libssp-0.dll libstdc++-6.dll mingwm10.dll miniupnpc.dll pthreadGC2.dll pthreadGCE2.dll zlib1.dll
运行命令行版本的bitcoin的client,看看帮助提示:
$ ./bitcoin-cli.exe --help Bitcoin RPC client version v0.8.2-820-gf65dc44-dirty-beta Usage: bitcoin-cli [options] <command> [params] Send command to Bitcoin server bitcoin-cli [options] help List commands bitcoin-cli [options] help <command> Get help for a command Options: -? This help message -conf=<file> Specify configuration file (default: bitcoin.conf) -datadir=<dir> Specify data directory -testnet Use the test network -rpcconnect=<ip> Send commands to node running on <ip> (default: 127.0.0 .1) -rpcwait Wait for RPC server to start -rpcuser=<user> Username for JSON-RPC connections -rpcpassword=<pw> Password for JSON-RPC connections -rpcport=<port> Connect to JSON-RPC on <port> (default: 8332 or testnet : 18332) SSL options: (see the Bitcoin Wiki for SSL setup instructions) -rpcssl Use OpenSSL (https) for JSON-RPC conn ections
7) 编译qt前端