Creating working dylibs on Mac OS X, pretty useful when developing software on Mac

Source: Internet
Author: User
Creating working dylibs

So creating a working dylib of one of your own libraries is not a very
Well known ented process. So I 've decided to write a short summary
Some things that will aid other people in creating their own dylibs
That they can just stick into their app package and have work.

I assume that you already have knowledge of how to add a framework and
Get it to show up in the frameworks folder in your app package. You can
Find out how to do this sort of stuff from other pages, so I won't take
The time to cover it here.

Some tools that will come in handy:
Otool-l
Install_name_tool
Libtool


So, Chances probably are that you 've figured out how to compile your
Library into a dylib already, but when you add it to your project
Builder project and compile your code, you find that the dylib is being
Properly put into the right folder (the frameworks folder) in your
Bundle. But your application won't work because it can't find
Dylib! What's up?

Well, the problem isn't actually with project Builder (it's actually
Sort of a bug with OS x, since it all worked properly in OS 9), it's
With the library. If you open up the terminal and CD into your app
Package and find the binary that's in the contents/MACOs folder, you
Can runOtool-lOn the binary that you find there, and you might see something like the following:

% Otool-l myfunbinary
Myfunbinary
/Usr/lib/libsystem. B. dylib (compatibility version 1.0.0, current version 71.0.0)
Libbz2.1.0.2.dylib (compatibility version 1.0.0, current version 1.0.2)

In this example, I'm trying to link in my own copy of The Bzip2 Library
Dylib that was created for me with Bzip2 and a patch that's available
On the Internet.

As it turns out, in OS X 10.0-10.2, you need the path to the library
To show up in there -- what you need is for the second line to show
Something like this:

 
@ Executable_path/../frameworks/libbz2.1.0.2.dylib (compatibility version 1.0.0, current version 1.0.2)

This tells OS X that it shoshould look in the place where the binary
is, move up a directory and into the framworks folder, and the library
will be sitting there. which it is. indeed, if you run otool-L on the
binary of some application in your/Applications folder that has a
working frameworks or dylibs in the app package frameworks folder (try
one of the applications from omni ), you can see that this is the case
-- all of their binaries comtain the right path. but the compiler is
not figuring things out right... in fact, as it is right now, You can
place the dylib in the same folder that. APP package is in, and
things will run properly. but that's not what we want.

Well, the first thing you can try to do is to use install_name_tool. If
You read the man page for install_name_tool, you can actually change
The information inside of the executable so that it has the right
Location. Depending on how the application was compiled, this may or
May not work correctly. More specifically, you'll be able to change
Location if there was extra space compiled into the binary to allow
Space for a longer pathname. Generally install_name_tool will work on
Binaries, but not libaries. So you can now do this:

% Install_name_tool-change libbz2.1.0.2.dylib @ executable_path/../frameworks/libbz2.1.0.2.dylib myfunbinary

Now try to run the file. Voila! It shoshould work.
However this is somewhat unacceptable, as whenever the binary gets
Changed -- taht is, whenever you recompile your code, this binary is
Going to get the short end of the shaft and get replaced by a new
Binary. Now, you cocould actually set up a shell script that runs
Install_name_tool every time you build your program, but that's
Somewhat of a hack. We might as well try to fix things at the origin
The problem.

As it turns out, the actual problem resides with the Library itself. If you take your library, and run otool-L on it like this:

 
% Otool-l libbz2.1.0.2.dylib

/Usr/lib/libsystem. B. dylib (compatibility version 1.0.0, current version 71.0.0)
Libbz2.1.0.2.dylib (compatibility version 1.0.0, current version 1.0.2)


Well, this looks familiar! Well, in fact the computer picks up
Location of the library from the library itself -- what we need to do
Is to get @ Executable_path/../frameworks/
To show up in front of the binary name. We can try using
Install_name_tool, but this time we find out that things don't work.
Why is this? There's no extra room compiled into the library to allow
For a longer pathname, so we 'd have to compile the original Library
With an extra flag to allow for more room in the path. But even better,
We might as well compile things correctly the firs time! Note that I
Can't give you exact directions ctions from this point on, since things will
Differ slightly for different applications you are compiling. But in
General, you shoshould go back to the source code for your application,
Find the linker line in the makefile that generates the dylib, and
Change it, adding -Install_name Flag to the libtool line. You'll want to do something like:

Libtool-dynamic-flat_namespace-install_name @ executable_path/../frameworks/libbz2.1.0.2.dylib \
-Lsystem-compatibility_version 1.0-current_version 1.0.2 \
-O libbz2.1.0.2.dylib-undefined suppress $ (objs)

Anyway, I hope that helps some people out. Good luck!
Also of interest may be the apple documentation on shared libraries and frameworks.

Related Article

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.