将 C++ 代码链接到 MacOS 中的 dylib 库

Linking C++ code to a dylib library in MacOS

我能够 setup BlockSci 在 MacOS High Sierra 10.13.6 上。安装程序在 /usr/local/include 中安装了头文件,在 /usr/local/lib 中安装了 libblocksci.dylib。我尝试编译的 C++ 代码是:

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

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

我为hello.cpp使用的编译命令是:

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

但是,找不到 BlockSci 库的符号:

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)

当我尝试编译这个时我做错了什么?

我发现 this and this 很有帮助。它最终编译为:

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

然而,现在我得到:

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

回到绘图板,但现在已经编译了。