【问题标题】:Linking C++ code to a dylib library in macOS将 C++ 代码链接到 macOS 中的 dylib 库
【发布时间】:2022-11-07 06:05:56
【问题描述】:

我能够在macOS v10.13(High Sierra)10.13.6 上setup BlockSci。安装程序在/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)

当我尝试编译它时,我做错了什么?

【问题讨论】:

  • 你告诉 g++在哪里找到图书馆,但你没有告诉它哪个图书馆。
  • 这回答了你的问题了吗? How to include needed C library using gcc?
  • 我尝试了g++ -std=c++17 -L/usr/local/lib -llibblocksci.dylib -I/usr/local/include/blocksci -I/usr/local/include/blocksci/external -o hello hello.cpp,但它仍然不起作用,出现错误ld: library not found for -llibblocksci.dylib clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • -l 选项是您所需要的,但它不像您想的那样工作。请阅读手册(可能是man gcc,不确定如何在您的系统上执行此操作,但您可以在网上找到)。是的,它很长,但它是可搜索的。我可以引用相关部分,但其中的乐趣在哪里?

标签: c++ linker g++ macos-sierra dylib


【解决方案1】:

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

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

回到绘图板,但它现在编译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多