如何让Xcode使用Boost Filesystem链接和调试应用程序?

Sim*_*ele 6 c++ macos xcode boost

TL; DR

Objective-C app与静态库链接,动态链接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++)
Run Code Online (Sandbox Code Playgroud)

为了获得所有链接,我将libboost_system和libboost_filesystem dylibs添加到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
Run Code Online (Sandbox Code Playgroud)

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

将它们放在Executables目录允许我从终端运行应用程序.

从Finder/Xcode运行时,如何让应用程序找到dylib?

背景资料

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

./b2 threading=multi macosx-version=10.7 --with-filesystem stage
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*eck 12

OSX上的动态库(dylib)在应该从中加载的路径中进行烘焙.例如...

/usr/lib/some_awesome.dylib.

链接到dylib时,链接器会将此路径嵌入可执行文件中,作为在运行时查找它的位置.使用已安装的库这很简单,但是对于相对路径链接,它更复杂.

当你构建boost库时,他们只是嵌入了他们的名字而不是完整或相对路径(即libboost_system.dylib而不是/usr/lib/libboost_filesystem.dylib).您应该可以使用该dll-path 选项更改此选项,但目前似乎已损坏.

要解决您的问题,您需要获取相对于您的应用程序嵌入(例如@executable_path/libwhatever.dylib)到dylibs 的正确路径,这可能需要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

以下是一篇很好的文章:创建工作dylibs