如何从shell编译在macOS Sierra上使用dylib路径的源代码

JPC*_*PCF 10 g++ dylib macos-sierra

我正在编译一些源代码,需要我已经构建的其他项目中的一些dylib.我越来越

ld:找不到架构x86_64`的符号

每当我执行

g++ some_code.cpp -I/usr/local/include -o executable_binary
Run Code Online (Sandbox Code Playgroud)

我知道g++无法找到已编译的dylib(安装在/usr/local/include),因为错误还提到了很多特定符号,这些符号是dylib的一部分.

我已经尝试过了:

  1. 执行 install_name_tool -id "@/usr/local/lib/requiredlib.dylib" /usr/local/lib/requiredlib.dylib
  2. 添加-L/usr/local/lib到编译选项.
  3. 将所有dylib路径显式添加到编译选项.
  4. 尝试添加DYLD_LIBRARY_PATH失败,因为Sierra出于安全原因不允许设置该变量.

我知道可以添加,DYLD_LIBRARY_PATH但需要禁用SIP.我可以做到这一点,如果有更清洁的方法,我不想这样做.

PS:我正在尝试编译郁金香图库的教程示例.

缺少的符号与我安装的图库有关.错误消息是:

Undefined symbols for architecture x86_64:
  "tlp::saveGraph(tlp::Graph*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, tlp::PluginProgress*)", referenced from:
      _main in tutorial001-02ee7e.o
  "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, tlp::Graph const*)", referenced from:
      _main in tutorial001-02ee7e.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

每当我这样做ls /usr/local/lib/requiredlib.dylib,郁金香的所有编译库都在那里.

g++ -v 生产:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Run Code Online (Sandbox Code Playgroud)

完成后,ls /usr/local/include/tulip/我得到了*.h我打算使用的库的文件列表.

Bal*_*zar 5

此外,您还可以查看undefined选项ld

指定如何处理未定义的符号。选项有:error、warning、suppress 或 dynamic_lookup。默认为错误。

-Wl,-undefined,dynamic_lookup在编译二进制文件时会调用它。

您还可以利用-lazy-lxor-lazy-library path以便在调用库中的第一个函数之前不加载库,这在某些情况下可能会有所帮助。

然后添加 rpath 标志,在用install_name_tool@macmoonshine 显示更改名称后,但请确保指向正确的路径。默认情况下,Tulip 安装在默认库文件夹中,/usr/local但在安装指南中建议将其安装在用户管理的目录中。

对于 tulip 所需的所有库,类似于以下命令。

install_name_tool -change ./build-release/lib/libtulip-core-4.11.dylib '@rpath/libtulip-core-4.11.dylib' tutorial001
Run Code Online (Sandbox Code Playgroud)

-Wl,-rpath,./build-release/lib在编译教程时使用。


cle*_*ens 3

您可以设置-rpath搜索库。链接二进制文件后,您必须修改库的搜索路径,例如:

g++ some_code.cpp -I/usr/local/include -o binary \
    -L/usr/local/lib -lrequiredlib -Wl,-rpath,/usr/local/lib
install_name_tool -change /usr/local/lib/librequiredlib.dylib \
    '@rpath/librequiredlib.dylib' binary
Run Code Online (Sandbox Code Playgroud)

install_name_tool命令更改二进制文件中库的名称,以便在 rpath 中搜索该库。如果您不确定正确的名称,请使用otool -L binary查看与可执行文件链接的所有库。

ld请参阅和的手册页install_name_tool以获取有关 rpath 的更多信息。也install_name_tool可以添加更多 rpath 。-add_rpath