Xcode and cocoa development: How to Use third-party dylib

Source: Internet
Author: User

Author: virushuo was last updated on
Copyright statement: You can reprint it as needed. During reprinting, you must mark the original source and author information of the article and this statement in the form of a hyperlink.
Http://blog.devep.net/virushuo/2009/07/01/xcodecocoadylib.html

 

UPDATE: The jjgod method in the message is more authentic and simple. See. However, if there is a dependency between dylib, you do not want to re-compile it. I 'd like to use this method.

Dylib is a BSD-style dynamic library. Basically, it can be considered as equivalent to Windows DLL and Linux so. Mac is based on BSD, so dylib is also used.

If you need to reference a third-party dylib and compile the cocoa program under xcode, running on the local machine will not cause problems. However, a problem may occur when it is released for use by other users. Because the user does not necessarily have this library.

This problem has caused me a lot of trouble, and I have not found a formal method yet. But I did solve this problem, although the method is not necessarily authentic. Write it down,If there is no better solution for the moment, do it first. If anyone has a better solution, please leave a message or email to me..

My solution is as follows:

1 otool-l yourapp. APP/contents/MACOs/yourapp

This step means to use the otool command for your compiled app to obtain information about dylib dependencies. Pay attention to this path. The cocoa app is displayed as a directory in the command line. Everything is in it.
The result is as follows:
Yourapp. APP/contents/MACOs/yourapp:
/System/library/frameworks/cocoa. Framework/versions/A/Cocoa (compatibility version 1.0.0, current version 12.0.0)
/Usr/local/lib/lib01.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/local/lib/lib02.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/local/lib/lib03.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Usr/lib/libsystem. B. dylib (compatibility version 1.0.0, current version 111.0.0)
/Usr/lib/libobjc. A. dylib (compatibility version 1.0.0, current version 227.0.0)
/System/library/frameworks/corefoundation. Framework/versions/A/corefoundation (compatibility version 150.0.0, current version 476.0.0)
/System/library/frameworks/appkit. Framework/versions/C/appkit (compatibility version 45.0.0, current version 949.0.0)
/System/library/frameworks/Foundation. Framework/versions/C/Foundation (compatibility version 300.0.0, current version 677.12.0)

Pay attention to the Red Place. If lib01, lib02, and lib03 are third-party libraries referenced by this program, their reference addresses are located on/usr/local/lib. This is the installation on the development machine. The client that uses this program may not install these things, so the program will go wrong.

Obviously, we need to do two things. A attaches these libraries to the app. B modifies their reference addresses to the correct location.

2 mkdir yourapp. APP/contents/dylib

In the compiled app, create the dylib directory.

Then copy all the required databases.

CP/usr/local/lib/lib01.dylib yourapp. APP/contents/dylib/
CP/usr/local/lib/lib02.dylib yourapp. APP/contents/dylib/
CP/usr/local/lib/lib03.dylib yourapp. APP/contents/dylib/

3 install_name_tool-change/usr/local/lib/lib01.dylib @ loader_path/../dylib/lib01.dylib "yourapp. APP/contents/MACOs/yourapp"

Install_name_tool is a command provided by Apple to modify the installation name of dylib. After this command is executed, you can use otool-L to see the changes.

Yourapp. APP/contents/MACOs/yourapp:
/System/library/frameworks/cocoa. Framework/versions/A/Cocoa (compatibility version 1.0.0, current version 12.0.0)
@ Loader_path/../dylib/lib01.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/local/lib/lib02.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/local/lib/lib03.dylib (compatibility version 0.0.0, current version 0.1.0)
/Usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
/Usr/lib/libsystem. B. dylib (compatibility version 1.0.0, current version 111.0.0)
/Usr/lib/libobjc. A. dylib (compatibility version 1.0.0, current version 227.0.0)
/System/library/frameworks/corefoundation. Framework/versions/A/corefoundation (compatibility version 150.0.0, current version 476.0.0)
/System/library/frameworks/appkit. Framework/versions/C/appkit (compatibility version 45.0.0, current version 949.0.0)
/System/library/frameworks/Foundation. Framework/versions/C/Foundation (compatibility version 300.0.0, current version 677.12.0)

Pay attention to the red position. It has changed. @ Loader_path refers to the location where the application is running, that is, yourapp. APP/contents/MACOs/yourapp. Therefore, you must use one .. to locate the dylib directory created in step 1.
Repeat this command to modify lib02 and lib03.

4 otool-l yourapp. APP/contents/dylib/*. dylib

Continue to use otool to check whether the third-party library used in dylib has other Dependencies and whether install_name is correct. Repeat steps 1, 2, and 3 to copy the required dylib and modify install_name.

If all the settings are correct, the app comes with dylib, which can be run correctly on other machines. You do not need to find the Libraries under/usr/local/lib.

The modified result is a build result. Of course, every build is very troublesome. So proceed as follows:

1. A complete dylib directory is obtained in the previous step. Copy this dylib as a backup. For example, put it under your xcode project.

2. Compile a script:

Mkdir "$ target_build_dir/$ product_name.app/contents/dylib"
CP-F/Your/path/to/xcode_project_name/dylib/*. dylib "$ target_build_dir/$ product_name.app/contents/dylib /"

Install_name_tool-change/usr/local/lib/lib01.dylib @ loader_path/../dylib/lib01.dylib "$ target_build_dir/$ product_name.app/contents/MACOs/$ product_name"
(Use this format to repeat the dylib used by the app)

3. In xcode, expand the targets node, right-click the project name, and choose add> new build phasa> new run script build phasa from the menu. In the displayed dialog box, paste the script ..

This script runs automatically after build. However, I have a strange problem here. If/bin/sh is written in the shell, the file cannot be found (actually exists) and the shell is empty, instead, you can run shell commands correctly.

After these operations, the compiled app can run on other machines. It's really troublesome...

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.