English 中文(简体)
• 如何在MacOS X(C++)中使用校正
原标题:How to use dylib in Mac OS X (C++)

I made an application (an executable) calling some dylib successfully. However, the dylib files and the executable are in different directory. I added the directory contains dylib files to the $PATH environment variable. However, it still doesn t load. I copy all the dylib files to the executable, the program finally runs. This confirms the dylib files have no problem. However, how can I tell the OS to find it?
In windows, I just need to add the directory path contains dll files to $PATH. What do I need to do for Mac OS X?

问题回答

在阅读了Justin提供的链接后,我成功地使用了@executable_path。 为了改变我的校正安装——名称指我可起诉的同一继承人。

@executable_path Absolute paths are annoying. Sometimes you want to embed a framework into an application instead of having to install the framework into /Library or a similar location.

The Mac s solution to this is @executable_path. This is a magic token that, when placed at the beginning of a library s install name, gets expanded to the path of the executable that s loading it, minus the last component. For example, let s say that Bar.app links against Foo.framework. If Bar.app is installed in /Applications, @executable_path will expand to /Applications/Bar.app/Contents/MacOS. If you intend to embed the framework in Contents/Frameworks, then you can just set Foo.framework s install name to @executable_path/../Frameworks/Foo.framework/Versions/A/Foo. The dynamic linker will expand that to /Applications/Bar.app/Contents/MacOS/../Frameworks/Foo.framework/Versions/A/Foo and will find the framework there.

rel=“noreferer” http://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html

我要举一个例子。

Let s say I have the following executable /opt/local/bin/convert and its dylibs are in /opt/local/lib. I want to copy it to another dir and have it load its dylibs from the same directory as where I copied the executable.

> mkdir ~/tmp/bin
> cp /opt/local/bin/convert ~/tmp/bin

2. 找到一份可执行人员死亡清单

> otool -L ~/tmp/bin/convert
~/tmp/bin/convert:
    /opt/local/lib/libtiff.3.dylib (compatibility version 13.0.0, current version 13.5.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    /opt/local/lib/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)
    /opt/local/lib/libfontconfig.1.dylib (compatibility version 6.0.0, current version 6.4.0)
    /opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
    /opt/local/lib/libfreetype.6.dylib (compatibility version 15.0.0, current version 15.0.0)
    /opt/local/lib/libexpat.1.dylib (compatibility version 7.0.0, current version 7.2.0)
    /opt/local/lib/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.6)
    /opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.6)
    ...

我只关心/opt/ local/lib dir”中的校正,因此我们只从/opt中抽出dy。 我想保留所有其他不准确之处,特别是/usr/lib/libSystem

> DYLIBS=`otool -L ~/tmp/bin/convert | grep "/opt" | awk -F     { print $1 } `

抄录可起诉的同一继承人的可执行内容。

> for dylib in $DYLIBS; do cp $dylib ~/tmp/bin/; done;

使用<代码>install_name_tool,改变我们在以上步骤中提取的所有校正名称,代之以将@executable_path预先贴在校正名称。 这样,动态联系人就能够找到与可起诉人所在地相同的目录中的校正。

> for dylib in $DYLIBS; do install_name_tool -change $dylib @executable_path/`basename $dylib` ~/tmp/bin/convert; done;

Confirm that the install names have been changed and that libSystem is still pointing to /usr/lib/libSystem.

> otool -L ~/tmp/bin/convert
~/tmp/bin/convert:
    @executable_path/libtiff.3.dylib (compatibility version 13.0.0, current version 13.5.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    @executable_path/libjpeg.8.dylib (compatibility version 12.0.0, current version 12.0.0)
    @executable_path/libfontconfig.1.dylib (compatibility version 6.0.0, current version 6.4.0)
    @executable_path/libiconv.2.dylib (compatibility version 8.0.0, current version 8.1.0)
    @executable_path/libfreetype.6.dylib (compatibility version 15.0.0, current version 15.0.0)
    @executable_path/libexpat.1.dylib (compatibility version 7.0.0, current version 7.2.0)
    @executable_path/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.6)
    @executable_path/libz.1.dylib (compatibility version 1.0.0, current version 1.2.6)
    ...

页: 1 环境变量

http://developer.apple.com/library/mac/#documentation/darwin/fer/manpages/man1/dyld.1.html

      This  is  a  colon  separated  list  of directories that contain libraries. The dynamic linker
      searches these directories before it searches the default locations for libraries.  It  allows
      you to test new versions of existing libraries.

      For  each  library  that  a program uses, the dynamic linker looks for it in each directory in
      DYLD_LIBRARY_PATH in turn. If it still can t find the library,  it  then  searches  DYLD_FALL-
      BACK_FRAMEWORK_PATH and DYLD_FALLBACK_LIBRARY_PATH in turn.

在许多方面。 也许这将有助于:

http://www.mikeash.com/pyblog/friday-qa-2009-11-06-linking-and-install-names.html>

如果伤残偿金在《国际民事责任法》规定的地点。 该图书馆的不结盟运动将以工作为基础。

Otherwise, you can add the location of the dylib to DYLD_LIBRARY_PATH. You might want to read the dyld documentation.

页: 1 ROOT_PATH/INSTALL_NAME,但与DYLD_ROOT_PATH混淆的情况非常少见。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签