IOS Compilation Deployment Path

Source: Internet
Author: User

The initial exposure to these variables on OSX is somewhat faint. After reading some documents, I found it clear. Make a summary.


When compiling a dynamic library such as libfoo.dylib, you need to specify Install_path. That is, its installation path.


An executable program, such as Bar.app, uses libfoo.dylib, so when compiling Bar.app, Libfoo.dylib Install_path is recorded in Bar.app, which is used to locate the dylib. Use the following command to view:


$ otool-l Bar.app/contents/macos/bar
Bar.app/contents/macos/bar:
/usr/local/lib/libfoo.dylib (Compatibility version 1.0.0, current version 1.0.0)
/system/library/frameworks/cocoa.framework/versions/a/cocoa (Compatibility version 1.0.0, current version 19.0.0)
/system/library/frameworks/foundation.framework/versions/c/foundation (Compatibility version 300.0.0, current Version 945.0.0)
...
The/usr/local/lib here is the default Install_path. For Bar.app to function properly, you must first copy the Libfoo.dylib to this directory. If Libfoo.dylib is only used by Bar.app, then copying to the system directory may not be appropriate. One way to solve this problem is to modify the Libfoo.dylib Install_path, using relative paths. Therefore, we need to use the following three variables.


Look at Dyld's manual, there are explanations of these three variables.


@executable_path this variable represents the directory in which the executable program resides. Like/path/bar.app/contents/macos/.


@loader_path this variable represents the directory where each binary (including executable, dylib, framework, and so on) is loaded. In one process, for each module, the @loader_path resolves to an unused path, and the @executable_path is always resolved to the same path (the directory where the executable program resides). For example, a plugin that will be called by multiple programs, located in/path/myfilter.plugin/contents/macos/myfilter, depends on/path/myfilter.plugin/contents/dylib/ Libfoo.dylib. Then the Libfoo.dylib Install_path can be set to @loader_path/. /dylib, this setting, regardless of where the Myfilter.plugin directory is placed, Libfoo.dylib can be loaded correctly.


@rpath differs from the previous two, it is just a variable that holds one or more paths. For example, Libfoo.dylib is used by two. App and contains different paths, as follows:


bar.app/
contents/
macos/
Bar
Libfoo.dylib


Baz.app
contents/
macos/
Baz
dylibs/
Libfoo.dylib
Set the Libfoo.dylib Install_path to @loader_path/. or @loader_path/. /dylibs can only satisfy one of the. App requirements. To solve this problem, you can use @rpath. Set the Install_path of the Libfoo.dylib to @rpath, and then, when compiling the Bar.app, specify the @rpath as @loader_path/..., @loader_path/... /dylibs, the problem has been solved. Another advantage of @rpath is that you can set multiple paths. If Bar.app also needs to use another. Framework (assuming its install_path is also set to @rpath), it is located @loader_path/. /frameworks, add this path to the @rpath.


The reason is clear, how to set these variables?
Set the Install_path setting to @rpath in the project properties of Libfoo.dylib


Set the @rpath in Bar.app to @loader_path/... @loader_path/dylibs

For a compiled binary, there are several command-line tools available to view and modify Rpath:


View @rpath


$ otool-l Bar.app/contents/macos/bar
...
Load Command 19
CMD Lc_rpath
Cmdsize 32
Path @loader_path/.. (Offset 12)
Load Command 20
CMD Lc_rpath
Cmdsize 40
Path @loader_path/.. /dylibs (offset 12)
...
Install_name_tool can also delete/replace/Add @rpath, such as:


$ Install_name_tool-add_rpath @loader_path/.. /frameworks Bar.app/contents/macos/bar
It can also modify the load path of a dependent dynamic library, such as:


$ otool-l Bar.app/contents/macos/bar
Bar.app/contents/macos/bar:
@rpath/libfoo.dylib (Compatibility version 1.0.0, current version 1.0.0)
...
$ install_name_tool-change @rpath/libfoo.dylib @loader_path/libfoo.dylib Bar.app/contents/macos/bar
$ otool-l Bar.app/contents/macos/bar
Bar.app/contents/macos/bar:
@loader_path/libfoo.dylib (Compatibility version 1.0.0, current version 1.0.0)
...
Look at Otool, Install_name_tool's help understanding the details of these commands

IOS Compilation Deployment Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.