C++

如何更新 Antonopoulos 書中的舊 C++ 程式碼以使用目前庫?

  • September 24, 2019

我正在嘗試從 Antonopoulos 的“Mastering Bitcoin”一書(第 69 頁)中用 C++ 編譯程式碼。

我對所有這些都是新手,包括 C++、python 等。我認為它是一種做一些有趣的事情的方式,並在它旁邊學習一些不同的語言。

該程式碼使用#include <bitcoin/bitcoin.hpp,我已經在我的 Raspberry Pi 上進行這項工作時遇到了麻煩。我libbitcoin原始碼安裝,然後我必須提供路徑libbitcoin.pc才能pkg-config執行以下命令:

g++ -o addr addr.cpp $(pkg-config --cflags -- libs libbitcoin)

然後編譯器給出以下錯誤:

69_addr.cpp: In function ‘int main()’:
69_addr.cpp:9:3: error: ‘ec_point’ is not a member of ‘bc’
  bc::ec_point public_key = bc::secret_to_public_key(secret);
  ^~
69_addr.cpp:10:34: error: ‘encode_hex’ is not a member of ‘bc’
  std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
                                 ^~
69_addr.cpp:10:49: error: ‘public_key’ was not declared in this scope
  std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;
                                                ^~~~~~~~~~

我懷疑自 2017 年 6 月這本書出版以來圖書館已經發生了變化。

這是我嘗試編譯的書中的程式碼:

#include <bitcoin/bitcoin.hpp>


int main()
{
 bc::ec_secret secret;
 bool success = bc::decode_base16(secret,"038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31v14776");
 assert(success);

 bc::ec_point public_key = bc::secret_to_public_key(secret);
 std::cout << "Public key: " << bc::encode_hex(public_key) << std::endl;

 const bc::short_hash hash = bc::bitcoin_short_hash(public_key);

 bc::data_chunk unencoded_address;
 unencoded_address.reserve(25);
 unencoded_address.push_back(0);
 bc::extend_data(unencoded_address, hash);
 bc::append_checksum(unencoded_address);
 assert(unencoded_address.size() == 25);
 const std::string address = bc::encode_base58(unencoded_address);

 std::cout << "Address: " << address << std::endl;
 return 0;
}

編輯:發布這個問題後不久,我從下面的答案中發現了github。我使用那裡的新程式碼進行編譯,現在我可以做更多事情,但它仍然以錯誤退出。這是完整的日誌,有問題的部分當然在底部並說undefinined referenceto __gmpn_set_strand __gmpn_gcdext

13:28:16 [bitcoin@raspberrypi BuchSkripte]$ g++ -v -o addr 69_addr2.cpp $(pkg-config --cflags --libs libbitcoin)
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 6.3.0-18+rpi1+deb9u1' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1+deb9u1) 
COLLECT_GCC_OPTIONS='-v' '-o' 'addr' '-I' '/home/pi/libbitcoin/include' '-L/home/pi/libbitcoin/lib' '-shared-libgcc' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
/usr/lib/gcc/arm-linux-gnueabihf/6/cc1plus -quiet -v -I /home/pi/libbitcoin/include -imultilib . -imultiarch arm-linux-gnueabihf -D_GNU_SOURCE 69_addr2.cpp -quiet -dumpbase 69_addr2.cpp -march=armv6 -mfloat-abi=hard -mfpu=vfp -mtls-dialect=gnu -auxbase 69_addr2 -version -o /tmp/cca8JFzr.s
GNU C++14 (Raspbian 6.3.0-18+rpi1+deb9u1) version 6.3.0 20170516 (arm-linux-gnueabihf)
   compiled by GNU C version 6.3.0 20170516, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=88 --param ggc-min-heapsize=109911
ignoring duplicate directory "/usr/include/arm-linux-gnueabihf/c++/6"
ignoring nonexistent directory "/usr/local/include/arm-linux-gnueabihf"
ignoring nonexistent directory "/usr/lib/gcc/arm-linux-gnueabihf/6/../../../../arm-linux-gnueabihf/include"
#include "..." search starts here:
#include <...> search starts here:
/home/pi/libbitcoin/include
/usr/include/c++/6
/usr/include/arm-linux-gnueabihf/c++/6
/usr/include/c++/6/backward
/usr/lib/gcc/arm-linux-gnueabihf/6/include
/usr/local/include
/usr/lib/gcc/arm-linux-gnueabihf/6/include-fixed
/usr/include/arm-linux-gnueabihf
/usr/include
End of search list.
GNU C++14 (Raspbian 6.3.0-18+rpi1+deb9u1) version 6.3.0 20170516 (arm-linux-gnueabihf)
   compiled by GNU C version 6.3.0 20170516, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=88 --param ggc-min-heapsize=109911
Compiler executable checksum: 79dca666fccaa96025c9107a8b3f60f3
COLLECT_GCC_OPTIONS='-v' '-o' 'addr' '-I' '/home/pi/libbitcoin/include' '-L/home/pi/libbitcoin/lib' '-shared-libgcc' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
as -v -I /home/pi/libbitcoin/include -march=armv6 -mfloat-abi=hard -mfpu=vfp -meabi=5 -o /tmp/ccryc6jT.o /tmp/cca8JFzr.s
GNU assembler version 2.28 (arm-linux-gnueabihf) using BFD version (GNU Binutils for Raspbian) 2.28
COMPILER_PATH=/usr/lib/gcc/arm-linux-gnueabihf/6/:/usr/lib/gcc/arm-linux-gnueabihf/6/:/usr/lib/gcc/arm-linux-gnueabihf/:/usr/lib/gcc/arm-linux-gnueabihf/6/:/usr/lib/gcc/arm-linux-gnueabihf/
LIBRARY_PATH=/usr/lib/gcc/arm-linux-gnueabihf/6/:/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/:/usr/lib/gcc/arm-linux-gnueabihf/6/../../../:/lib/arm-linux-gnueabihf/:/lib/:/usr/lib/arm-linux-gnueabihf/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'addr' '-I' '/home/pi/libbitcoin/include' '-L/home/pi/libbitcoin/lib' '-shared-libgcc' '-march=armv6' '-mfloat-abi=hard' '-mfpu=vfp' '-mtls-dialect=gnu'
/usr/lib/gcc/arm-linux-gnueabihf/6/collect2 -plugin /usr/lib/gcc/arm-linux-gnueabihf/6/liblto_plugin.so -plugin-opt=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper -plugin-opt=-fresolution=/tmp/cc0zjaUW.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --sysroot=/ --build-id --eh-frame-hdr -dynamic-linker /lib/ld-linux-armhf.so.3 -X --hash-style=gnu -m armelf_linux_eabi -o addr /usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o /usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crti.o /usr/lib/gcc/arm-linux-gnueabihf/6/crtbegin.o -L/home/pi/libbitcoin/lib -L/usr/lib/gcc/arm-linux-gnueabihf/6 -L/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf -L/usr/lib/gcc/arm-linux-gnueabihf/6/../../.. -L/lib/arm-linux-gnueabihf -L/usr/lib/arm-linux-gnueabihf /tmp/ccryc6jT.o -lbitcoin -lboost_chrono -lboost_date_time -lboost_filesystem -lboost_iostreams -lboost_locale -lboost_log -lboost_program_options -lboost_regex -lboost_system -lboost_thread -lpthread -lrt -ldl -lsecp256k1 -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/arm-linux-gnueabihf/6/crtend.o /usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crtn.o
/home/pi/libbitcoin/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o): In function `secp256k1_num_mod_inverse':
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:128: undefined reference to `__gmpn_gcdext'
/home/pi/libbitcoin/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o): In function `__gmpn_sub':
/usr/include/arm-linux-gnueabihf/gmp.h:2190: undefined reference to `__gmpn_sub_n'
/home/pi/libbitcoin/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o): In function `secp256k1_num_set_bin':
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49: undefined reference to `__gmpn_set_str'
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49: undefined reference to `__gmpn_set_str'
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49: undefined reference to `__gmpn_set_str'
/home/pi/libbitcoin/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o): In function `secp256k1_num_get_bin':
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:34: undefined reference to `__gmpn_get_str'
/home/pi/build-libbitcoin/secp256k1/src/num_gmp_impl.h:34: undefined reference to `__gmpn_get_str'
collect2: error: ld returned 1 exit status

我不確定這是特定於我的設置還是一般問題,但我打開了一個問題:https ://github.com/bitcoinbook/bitcoinbook/issues/643

我剛剛發現有一個github更新了程式碼。

具體程式碼addr.cppthis

編譯適用於--static例如g++ -o addr addr.cpp $(pkg-config --cflags --libs --static libbitcoin)

引用自:https://bitcoin.stackexchange.com/questions/90239