libbitcoin / libbitcoin-system

Bitcoin Cross-Platform C++ Development Toolkit
https://libbitcoin.info/
Other
1.29k stars 377 forks source link

problem to compil code addr.cpp #1162

Closed xubu1957 closed 4 years ago

xubu1957 commented 4 years ago

Trying to compile with g++ -o addr addr.cpp -std=c++11 $(pkg-config --cflags --libs libbitcoin) is getting me this error:

/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « secp256k1_num_mod_inverse » :
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:128 : référence indéfinie vers « __gmpn_gcdext »
/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « __gmpn_sub » :
/usr/include/x86_64-linux-gnu/gmp.h:2190 : référence indéfinie vers « __gmpn_sub_n »
/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « secp256k1_num_set_bin » :
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49 : référence indéfinie vers « __gmpn_set_str »
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49 : référence indéfinie vers « __gmpn_set_str »
/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « secp256k1_num_get_bin » :
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:34 : référence indéfinie vers « __gmpn_get_str »
/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « secp256k1_num_set_bin » :
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49 : référence indéfinie vers « __gmpn_set_str »
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:49 : référence indéfinie vers « __gmpn_set_str »
/home/user/myprefix/lib/libsecp256k1.a(libsecp256k1_la-secp256k1.o) : Dans la fonction « secp256k1_num_get_bin » :
/home/user/build-libbitcoin/secp256k1/src/num_gmp_impl.h:34 : référence indéfinie vers « __gmpn_get_str »
collect2: error: ld returned 1 exit status

Help me .

xubu1957 commented 4 years ago

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

#include <bitcoin/bitcoin.hpp>

int main()
{
    // Private secret key string as base16
    bc::ec_secret decoded;
    bc::decode_base16(decoded,
        "038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776");

    bc::wallet::ec_private secret(
        decoded, bc::wallet::ec_private::mainnet_p2kh);

    // Get public key.
    bc::wallet::ec_public public_key(secret);
    std::cout << "Public key: " << public_key.encoded() << std::endl;

    // Create Bitcoin address.
    // Normally you can use:
    //    bc::wallet::payment_address payaddr =
    //        public_key.to_payment_address(
    //            bc::wallet::ec_public::mainnet_p2kh);
    //  const std::string address = payaddr.encoded();

    // Compute hash of public key for P2PKH address.
    bc::data_chunk public_key_data;
    public_key.to_data(public_key_data);
    const auto hash = bc::bitcoin_short_hash(public_key_data);

    bc::data_chunk unencoded_address;
    // Reserve 25 bytes
    //   [ version:1  ]
    //   [ hash:20    ]
    //   [ checksum:4 ]
    unencoded_address.reserve(25);
    // Version byte, 0 is normal BTC address (P2PKH).
    unencoded_address.push_back(0);
    // Hash data
    bc::extend_data(unencoded_address, hash);
    // Checksum is computed by hashing data, and adding 4 bytes from hash.
    bc::append_checksum(unencoded_address);
    // Finally we must encode the result in Bitcoin's base58 encoding.
    assert(unencoded_address.size() == 25);
    const std::string address = bc::encode_base58(unencoded_address);

    std::cout << "Address: " << address << std::endl;
    return 0;
}
evoskuil commented 4 years ago

Libsecp256k1 is missing gmp, an optional dependency. How did you build it?

thecodefactory commented 4 years ago

Closed due to inactivity.

GeoffWilliams commented 3 years ago

I have the same problem with libbitcoin-system:

commit 9b75717682606ba6c92642db75d433cd9b30b07f (HEAD -> version3, origin/version3)

on Ubuntu 20.04.1 amd64

I built with:

sudo ./install.sh  --build-boost --disable-shared

pkg-config shows:

echo $(pkg-config --cflags --libs --static libbitcoin) 
-I/usr/local/include -L/usr/local/lib -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 -lgmp -lsecp256k1

Workaround

Add gmp to pkg-config (or add -lgmp)

g++ -std=c++11 -o addr addr.cpp $(pkg-config --cflags --libs --static libbitcoin gmp)  # works
evoskuil commented 3 years ago

GMP is not a dependency unless one makes it so via the conditions of the libsecp256k1 build.

GeoffWilliams commented 3 years ago

I just built whatever install.sh makes with the standard options from readme. The other strange thing is pkg-config already outputs -lgmp but it seems necessary to add another -lgmp to the end of the g++ command. Probably a c++ noob error on my part here

evoskuil commented 3 years ago

Did you direct it to build libsecp256k1 or did you rely on an installed package?

evoskuil commented 3 years ago

Because the build of libsecp256k1 you have installed has a gmp dependency.