Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: liorko87 on February 01, 2021, 03:01:12 PM



Title: Linking error when using bitcoin libs
Post by: liorko87 on February 01, 2021, 03:01:12 PM
I'm trying to build a protocol that uses bitcoin headers and libraries. When I'm creating a PKhash object from a publick key, I'm receiving this error:

Code:
/usr/bin/ld: /home/user/bitcoin/src/libbitcoin_common.a(libbitcoin_common_a-standard.o): in function `CHash160::CHash160()':
/home/user/bitcoin/src/./hash.h:46: undefined reference to `CSHA256::CSHA256()'

The code I created the hash from it from:

Code:
std::vector<unsigned char >byteRepr(tag.c_str(), tag.c_str() + tag.length());
CPubKey key(byteRepr);
PKHash hash (key);

My CMake file:

Code:
cmake_minimum_required(VERSION 3.16)
project(Bitcoinextension)

set(CMAKE_CXX_STANDARD 11)
set(${CMAKE_CXX_FLAGS} "${CMAKE_CXX_FLAGS} -g3")

include_directories($ENV{HOME}/bitcoin/src $ENV{HOME}/bitcoin/depends/x86_64-pc-linux-gnu/include)
link_directories($ENV{HOME}/bitcoin/src)

add_executable(Bitcoinextension main.cpp Pob.cpp Address.cpp)

target_link_libraries(Bitcoinextension ssl crypto
        $ENV{HOME}/bitcoin/src/libbitcoin_common.a
        $ENV{HOME}/bitcoin/src/libbitcoin_consensus.a
        $ENV{HOME}/bitcoin/src/libbitcoin_cli.a)


Title: Re: Linking error when using bitcoin libs
Post by: NotATether on February 02, 2021, 12:11:06 AM
You are sourcing include files from the bitcoin source tree without also adding the intermediate static libraries generated by Core during compilation that implement the header file(s) you're including.

In particular, the CSHA256 class lives in src/crypto/sha256.h\cpp which get compiled into src/crypto/libbitcoin_crypto_base.a , a static library that's not exported to /lib after compilation so put this file in your target_link_libraries as well.

Because Core uses makefiles and not CMake, you can't just set CMake to include the whole project folder as a dependency so you have to correlate header files you use with their required static libraries which you can find in src/Makefile.am (https://github.com/bitcoin/bitcoin/blob/master/src/Makefile.am)