设为首页 收藏本站
查看: 613|回复: 0

[经验分享] Creating working dylibs on Mac OS X, pretty useful When developing software on M

[复制链接]

尚未签到

发表于 2015-12-31 02:55:34 | 显示全部楼层 |阅读模式
Creating working dylibs
  
So creating a working dylib of one of your own libraries is not a very
well documented process. So I've decided to write a short summary of
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 the
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 run otool -L on 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 should 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 the .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 the
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 should 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 could 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 of
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 the
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 from this point on, since things will
differ slightly for different applications you are compiling. But in
general, you should go back to the source code for your application,
find the linker line in the Makefile that generates the dylib, and
change it, adding the -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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-158590-1-1.html 上篇帖子: mac os x 启用apache 和 php 下篇帖子: 如何修改 Mac OS X 上的 Home End 键的默认行为
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表