3

I am trying to compile a code in C++ from the Book "Mastering Bitcoin" by Antonopoulos (page 69).

I am new to all of this, including C++, python, etc. I see it as a way of doing something interesting and alongside it learning a bit about different languages.

The code uses #include <bitcoin/bitcoin.hpp and I already had troubles making this work on my Raspberry Pi. I installed libbitcoin from source and then I had to give the path of libbitcoin.pc to the pkg-config to be able to execute the following command:

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

The compiler then gives the following errors:

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;
                                                 ^~~~~~~~~~

I suspect there have been changes to the libraries since the book came out in June 2017.

This is the code from the book I try to compile:

#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;
}

EDIT: Shortly after posting this question I found out about the github from my answer below. I used the new code from there to compile and now I am able to do more but still it exits with error. Here is the full log, the problematic parts are of course at the bottom and say undefinined reference to __gmpn_set_str and __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

I am not sure if this is a problem specific to my setup or in general but I opened an issue: https://github.com/bitcoinbook/bitcoinbook/issues/643

1 Answer 1

2

I just found out there is a github where the code is updated.

The specific code for addr.cpp is this.

Compilation works with --static e.g. g++ -o addr addr.cpp $(pkg-config --cflags --libs --static libbitcoin).

1
  • You can add -lgmp when it happens
    – daparic
    May 19, 2021 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.