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

2024-05-14

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

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

每当我执行

g++ some_code.cpp -I/usr/local/include -o executable_binary

我知道g++无法找到已编译的 dylibs(安装在/usr/local/include)因为错误还提到了许多属于 dylib 的特定符号。

我已经尝试过这个:

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

我知道可以添加DYLD_LIBRARY_PATH但这需要禁用 SIP。如果有更干净的方法来做到这一点,我可以通过“我不想”来做到这一点。

P.S.:我正在尝试编译教程示例郁金香图库 http://tulip.labri.fr/TulipDrupal/.

缺少的符号与我安装的图形库有关。错误信息是:

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

每当我这样做时ls /usr/local/lib/requiredlib.dylibTulip 的所有编译库都在那里。

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

做完之后ls /usr/local/include/tulip/我得到的名单*.h我打算使用的库的文件。


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

指定如何处理未定义的符号。选项有:错误、警告、抑制或动态查找。默认是错误。

你会这样称呼-Wl,-undefined,dynamic_lookup编译二进制文件时。

您还可以利用-lazy-lx or -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

并且还使用-Wl,-rpath,./build-release/lib在编写教程时。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 shell 编译 macOS Sierra 上使用 dylib 路径的源代码 的相关文章

随机推荐