c++linkerg++macos-sierradylib

Linking C++ code to a DYLIB library in macOS


I was able to setup BlockSci on macOS v10.13 (High Sierra) 10.13.6. The setup installed header files in /usr/local/include and a libblocksci.dylib in /usr/local/lib. The C++ code I am trying to compile is:

#include "blocksci.hpp"
#include <iostream>
#include <string>

int main(int argc, const char * argv[]) {
    blocksci::Blockchain chain{"path/config.json"};
    return 0;
};

The compile command I am using for hello.cpp is:

g++ -std=c++17 -L/usr/local/lib -I/usr/local/include/blocksci -I/usr/local/include/blocksci/external -o hello hello.cpp

However, the symbols for the BlockSci library are not found:

Undefined symbols for architecture x86_64:
  "blocksci::Blockchain::Blockchain(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      _main in hello-942a60.o
  "blocksci::Blockchain::~Blockchain()", referenced from:
      _main in hello-942a60.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What am I doing wrong when I try to compile this?


Solution

  • I found this and this that were helpful. It finally compiled with:

    g++ hello.cpp -std=c++17 -I/usr/local/include/blocksci/external -o hello -L/usr/local/lib -lblocksci -Wl,-rpath,/usr/local/lib
    

    However, now I get a runtime error:

    libc++abi.dylib: terminating with uncaught exception of type std::runtime_error
    Abort trap: 6
    

    Back to the drawing board, but it compiled for now.