c++ - 如何让 Xcode 链接和调试带有 Boost 文件系统的应用程序?

标签 c++ xcode macos boost

长话短说

Objective-C 应用程序与动态链接 Boost 文件系统的静态库链接。应用程序可以使用终端从输出目录运行,但尝试从 Xcode 调试器或 Finder 运行会出现错误 dyld: Library not loaded: libboost_filesystem.dylib <snip> Reason: image not found .

问题

在我的 Xcode 项目中,我有一个如下所示的结构:

MainProject (Objective-C)
 - static_lib_that_uses_filesystem (C++)

为了链接所有内容,我将 libboost_system 和 libboost_filesystem 动态库添加到 MainProject 中的“Link Binary with Libraries”构建阶段。

当我尝试从 Xcode 或 Finder 运行应用程序时,我得到:

sharedlibrary apply-load-rules all
warning: Unable to read symbols for libboost_filesystem.dylib (file not found).
warning: Unable to read symbols from "libboost_filesystem.dylib" (not yet mapped into memory).
warning: Unable to read symbols for libboost_system.dylib (file not found).
warning: Unable to read symbols from "libboost_system.dylib" (not yet mapped into memory).
[Switching to process 43957 thread 0x0]
dyld: Library not loaded: libboost_filesystem.dylib
  Referenced from: /Users/ssteele/Library/Developer/Xcode/DerivedData/MainProject-dqrhyuarllykslftblocjdzxlran/Build/Products/Debug/MainProject.app/Contents/MacOS/MainProject
  Reason: image not found

我添加了一个构建阶段以将 dylib 复制到包中的 Frameworks 目录,这没有帮助。我将其更改为将它们复制到 Executables 目录,这也没有帮助。

将它们放在可执行文件目录中确实允许我从终端运行该应用程序。

如何让应用在从 Finder/Xcode 运行时找到动态库?

背景信息

我在 Lion 上使用 Xcode 4.2,目前仅针对 Lion。我像这样为文件系统构建共享库:

./b2 threading=multi macosx-version=10.7 --with-filesystem stage

这会在 stage/lib 目录中创建 libboost_system.dylib、libboost_filesystem.dylib 以及 .a 等价物,我直接从那里在项目中引用它们。

最佳答案

OSX 上的动态库 (dylib) 烘焙在它们应该从中加载的路径中。例如……

/usr/lib/some_awesome.dylib

当您链接到 dylib 时,链接器会将此路径嵌入到您的可执行文件中,作为在运行时查找它的位置。对于已安装的库,这很好且容易,但对于相对路径链接,它会更复杂。

当你构建 boost 库时,它们只是嵌入了它们的名称而不是完整或相对路径(即 libboost_system.dylib 而不是 /usr/lib/libboost_filesystem.dylib)。您应该能够使用 dll-path option 更改它, 但是 that seems broken currently .

要解决您的问题,您需要以某种方式将相对于您的应用程序的正确路径(例如 @executable_path/libwhatever.dylib)嵌入到 dylib 中,这可能需要 bjam dll 路径选择工作,或者您可以修复您的可执行文件以在不同的位置查找。

为此,请在您的构建中使用类似于以下内容的脚本步骤:

install_name_tool -change libboost_filesystem.dylib @executable_path/libboost_filesystem.dylib$BUILT_PRODUCTS_DIR/$EXECUTABLE_PATH

请注意,如果您有多个动态库,它们通过损坏的路径相互引用,您也需要修复它们之间的路径,例如

install_name_tool -change libboost_system.dylib @executable_path/libboost_system.dylib$BUILT_PRODUCTS_DIR/$EXECUTABLE_FOLDER_PATH/libboost_filesystem.dylib

以下是一篇关于此的好文章:Creating working dylibs

关于c++ - 如何让 Xcode 链接和调试带有 Boost 文件系统的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902420/

相关文章:

objective-c - 在坐标处绘制窗口或在 NSStatus 项之外绘制

c++ - 带按值传递参数的 std::forward

C++ STD 容器:std::vector - 有没有办法清除 vector 并保留存储分配?

c++ - FSM 中的状态应该与上下文类型成为 friend 吗?

Xcode 在最近的项目列表中不显示最近的项目

xcode - 我的 Mac OS X Cocoa 文档在哪里?

c++ - std::move() 和 std::add_rvalue_reference() 的区别

ios - 将 PDF 转换为 UIImageView

ios - Xcode 错误线程 1 : EXC_BREAKPOINT (code=1, 子代码 = 0x1001f276588)

python - 在安装了 Xcode 4 的 Mac Snow Leopard 上安装 PIL 的最佳方法是什么?