0

the libbitcoin library seems to have been installed just fine. but the following code can't seem to include bitcoin/bitcoin.hpp

#include<bitcoin/bitcoin.hpp>
#include<iostream>
using namespace bc;
int main() {
    block_type blk = genesis_block();
    std::cout<<encode_hex(hash_block_header(blk.header))<<std::endl;
    return 0;
}

Error:

g++ -o test test.cpp $(pkg-config --cflags --libs libbitcoin)
test.cpp: In function ‘int main()’:
test.cpp:5:5: error: ‘block_type’ was not declared in this scope
     block_type blk = genesis_block();
     ^~~~~~~~~~
test.cpp:5:5: note: suggested alternative: ‘clock_t’
     block_type blk = genesis_block();
     ^~~~~~~~~~
     clock_t
test.cpp:6:45: error: ‘blk’ was not declared in this scope
     std::cout<<encode_hex(hash_block_header(blk.header))<<std::endl;
                                             ^~~
test.cpp:6:45: note: suggested alternative: ‘brk’
     std::cout<<encode_hex(hash_block_header(blk.header))<<std::endl;
                                             ^~~
                                             brk
test.cpp:6:27: error: ‘hash_block_header’ was not declared in this scope
     std::cout<<encode_hex(hash_block_header(blk.header))<<std::endl;
                           ^~~~~~~~~~~~~~~~~
test.cpp:6:16: error: ‘encode_hex’ was not declared in this scope
     std::cout<<encode_hex(hash_block_header(blk.header))<<std::endl;
                ^~~~~~~~~~

3 Answers 3

0

I was able to run this code:

#include <bitcoin/bitcoin.hpp>

int main() 
{
  bc::ec_secret decoded;
  bc::decode_base16(decoded, "038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b");
}

Follow this https://github.com/libbitcoin/libbitcoin#debianubuntu

After compile it in a /home/x/dir run this:

g++ helloworld.cpp -I/home/dev/bitcoin-lib/include /home/dev/bitcoin-lib/lib/libbitcoin.a /home/dev/bitcoin-lib/lib/libboost_system.a /home/dev/bitcoin-lib/lib/libbitcoin.a /home/dev/bitcoin-lib/lib/libboost_program_options.a /home/dev/bitcoin-lib/lib/libboost_regex.a
  • we need to tell g++ where are the headers for our #include statement
  • We need to tell g++ what are the libraries: libbitcoin.a, etc
1
  • Don't forget -lgmp when it happens
    – daparic
    Commented May 19, 2021 at 11:20
1

Try adding the -std=c++11 flag.

I had the same problem and the following worked for me:

g++ -std=c++11 -o test test.cpp $(pkg-config --cflags --libs libbitcoin)
1
  • 1
    it didn't work!
    – Ang
    Commented Jun 16, 2018 at 13:27
1

The book code examples has an accompanying actively maintained github repo here. However, extra care is needed for the libbitcoin library dependency as it has new breaking changes. For example, #include <bitcoin/bitcoin.cpp> has been rename to #include <bitcoin/system.hpp>. And no more bc::ec_point. It is possible to install an older compatible version of libbitcoin library, but I prefer to use the latest version and made some adjustment in addr.cpp example code.

I manually build and installed the libbitcoin library to a custom location as suggested:

mkdir -p /opt/my/libbitcoin/
$ wget https://raw.githubusercontent.com/libbitcoin/libbitcoin/version3/install.sh
$ chmod +x install.sh
$ ./install.sh --prefix=/opt/my/libbitcoin/ --build-boost --disable-shared

The way I now build addr.cpp is thus:

export PKG_CONFIG_PATH=/opt/my/libbitcoin/lib/pkgconfig/
g++ -o addr addr.cpp $(pkg-config --cflags --libs libbitcoin-system) -lgmp

And then, running the executable:

$ ./addr
Public key: 0202a406624211f2abbdc68da3df929f938c3399dd79fac1b51b0e4ad1d26a47aa
Address: 1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK

Your Answer

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

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