【问题标题】:How can I get Xcode to link and debug an app with Boost Filesystem?如何让 Xcode 使用 Boost Filesystem 链接和调试应用程序?
【发布时间】:2012-02-12 17:12:30
【问题描述】:

TL;DR

Objective-C 应用程序与动态链接 Boost Filesystem 的静态库链接。可以使用终端从输出目录运行应用程序,但尝试从 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 dylib 添加到 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 运行时,如何让应用找到 dylib?

背景信息

我在 Lion 上使用 Xcode 4.2,目前仅针对 Lion。我为文件系统构建了我的共享库,如下所示:

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

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

【问题讨论】:

  • otool -l 的输出包括:Load command 11 cmd LC_LOAD_DYLIB cmdsize 56 name libboost_filesystem.dylib (offset 24) time stamp 2 Thu Jan 1 01:00:02 1970 current version 0.0.0 compatibility version 0.0.0
  • 我想知道该名称是否应该包含某种 @ 引用,例如 @executable_path/libboost_filesystem.dylib。不过,我似乎无法做到这一点。

标签: c++ xcode macos boost


【解决方案1】:

OSX 上的动态库(dylibs)在它们应该被加载的路径中烘焙。比如……

/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-path 选项才能工作,或者您可以修复您的可执行文件以查看不同的位置。

为此,请在构建中使用以下内容作为脚本步骤:

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

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

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

以下是这方面的好文章:Creating working dylibs

【讨论】:

  • 谢谢,使用 install_name_tool 似乎可以满足我的要求!
【解决方案2】:

问题是需要安装 boost,例如b2 ..... install。这会将库和头文件复制到 /usr/local/lib 和 /usr/local/include 中。

OSX 动态库只能从它们构建的目录运行。

您可以使用 -prefix 参数来更改安装目录以增强构建。但是,对于所有用户,这些库仍需要位于同一目录中。

应该有一种方法可以将 boost 构建为框架和/或在库中嵌入 @executable_path。

另一种方法是使用 boost 的静态库 - 仅构建静态库或删除动态库,Xcode 在静态之前查找动态。如果使用静态,则库的路径在运行时无关紧要,因为所有代码现在都在可执行文件中。

【讨论】:

    【解决方案3】:

    我认为您需要在“库搜索路径”中添加保存 libboost_filesystem.a 的目录的路径

    点击你的项目配置文件->构建设置->展开“搜索路径”->“库搜索路径”

    【讨论】:

    • 对不起,我已经在库搜索路径中有stage/lib目录。
    • 不工作。在库搜索路径中添加了我的 /usr/local/lib。还是没有成功。
    猜你喜欢
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多