如何使带有自制程序库的Xcode项目可移植?
我已经使用Brew在Mac上安装了FreeType. Mac上的代码工作正常,但是当我尝试在其他Mac上运行项目时,出现以下提到的链接错误.
I have installed FreeType on my mac using Brew. The code on my mac works fine, but when I try to run the project on other mac I get the below mentioned linking error.
dyld: Library not loaded: /usr/local/opt/freetype/lib/libfreetype.6.dylib
Referenced from: /Users/ashutosh/Library/Developer/Xcode/DerivedData/InstrumentChamp-
etytleaabhxmokadgrjzbgtmwxfl/Build/Products/Debug/instrumentchamp.app/Contents/MacOS/instrumentchamp
Reason: image not found (lldb)
当我尝试在其他Mac上运行代码时,所有库目录以及Freetype的包含目录都包含在项目的$ SRCROOT/目录中.
All the library directories and include directories of Freetype are included in project's '$SRCROOT/' directory when I try to run the code on other mac.
在库链接错误中看到的路径是brew在我创建此项目的Mac中安装了freetype的位置.
The path you see in the linking error for library is where brew had installed freetype in the mac where I created this project.
/usr/local/opt/freetype/lib/libfreetype.6.dylib
我已将所有需要的lib/include/目录复制到项目的主文件夹中.
我已经设置了库,并在Xcode中包含了路径.
I have copied all the lib/ include/ directories that were needed to my project's home folder.
And I have set the library and include paths in Xcode.
我在这里想念的是什么?为了使我的代码可在任何其他Mac上移植,我还需要做些什么. 通过安装Brew,我可以在其他Mac上运行该项目,但我想无需安装brew.
What is it that I am missing here? What else do I have to do to make my code portable on any other Mac. I got the project to run on other mac by installing Brew, but I want to do it without the need to install brew.
PS:由于无法为32位处理器的freetype编译.dylib,因此我不得不使用brew安装freetype,因为.dylib的64bit副本给了我诸如错误的体系结构"之类的错误!
PS: I had to install freetype using brew, as I couldn't compile the .dylib for freetype for 32bit processor, a 64bit copy of .dylib was giving me error such as 'wrong architecture!'
我在评论中得到的基本想法是,OS X在搜索库的位置上非常愚蠢,它将使用在搜索过程中使用的相同绝对路径编译以在运行时解决它们.
The basic idea I was getting at in my comments is that OS X is pretty stupid about where it searches for libraries, it will use the same absolute path used during compilation to resolve them at run-time.
通常,当您要将应用程序部署/分发到与构建它的计算机不同的计算机时,您会将库包含在安装软件包/捆绑包中.但是您可能希望他们在运行时使用相对于您的应用程序的路径,因此install_name_tool -change允许您将相对讨厌的绝对路径替换为相对的路径.
Usually when you want to deploy/distribute your application to a different machine from the one that built it, you will include your libraries with the install package/bundle. But you probably want them to use a path relative to your application at run-time, thus install_name_tool -change allows you to replace the nasty absolute path with something relative.
希望这很有道理,Apple使得在OS X上使用系统范围的框架真的非常容易,而自定义库则没有那么多.如果使用系统范围的框架进行编译,则/System/Library/Frameworks/...
在所有OS X安装中都是通用的(给定相同的目标发行版).
Hope this makes sense, Apple makes it really easy to use system-wide frameworks on OS X, custom libraries not so much. If you compile using a system-wide framework, /System/Library/Frameworks/...
is universally available on all OS X installs (given the same target release version).
为了解决您的问题,我将执行以下操作:
To solve your problem, I would do the following:
install_name_tool -change /usr/local/opt/freetype/lib/libfreetype.6.dylib @executable_path/lib/libfreetype.6.dylib <executable_name_here>
然后,它将停止在编译软件时所处的位置中寻找libfreetype.6.dylib,而是在运行时相对于可执行文件的位置进行搜索(在这种情况下,在子目录lib/
).
Then it will stop looking for libfreetype.6.dylib in the location it was when you compiled the software, and instead search for it relative to your executable's location at run-time (in this case, in the sub-directory lib/
).