我可以在我的可执行文件中包含dylib -s吗? [英] Can I include dylib-s in my executable?

查看:123
本文介绍了我可以在我的可执行文件中包含dylib -s吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(是的,我知道dylib的全部意义在于它是动态加载的,但是我正在尝试创建一个自包含的包.)

(Yes, I understand that the whole point of dylib is that it loads dynamically, but I'm trying to create a self contained package.)

如果有的话,我有一个从命令行生成的可执行文件-在OS-X/Lion上.我将可执行文件交付给一位朋友,但他无法运行它,因为他没有安装库.他宁愿安装这些库,所以现在我试图创建一个包含原始可执行文件以及所有需要的库的软件包.

I've got an executable that I built from the command line -- on OS-X/Lion, if it matters. I delivered the executable to a friend, but he can't run it because he doesn't have the libraries installed. He'd rather not install the libraries, so now I'm trying to create a package that includes the original executable plus all the needed libs.

我曾经使用过XCode(IDE),并且对make和命令行构建工具及其选项不是很熟悉. (我根据网络上的很好的说明构建了此工具.)因此,明确的说明会有所帮助.

I'm used to working in XCode (IDE) and am not very familiar with make and command-line build tools and their options. (I built this tool following very good instructions from the web.) Hence, explicit instructions would be helpful.

谢谢!

推荐答案

您不能直接将dylib放在可执行文件中. (这就是发明捆绑软件的目的,但这对命令行工具没有帮助.)

You can't directly put a dylib inside an executable. (That's pretty much what bundles were invented for—but that doesn't help you with command-line tools.)

您可以将每个dylib重建为静态(.a)库,在这种情况下,可执行文件所需的所有代码都将被复制到可执行文件中,而您无需分发任何内容.如果您已经找到了库的源代码,这通常很容易,但是在不知道确切的构建方式的情况下,很难告诉您要更改的内容.

You can rebuild each dylib as a static (.a) library, in which case all the code your executable needs will be copied into the executable, and you don't need to distribute anything with it. If you've got the source to the libraries, this is usually very easy—but without knowing exactly how you're building things, it's hard to tell you what to change.

静态链接要记住的一件事是,它会影响不同许可证一起播放的方式.特别是,如果您使用的任何库都是LGPL许可的,则静态链接它们会带来动态链接所不具有的后果.有关更多详细信息,请参见此问题(以及答案中的链接),但实际上,您不应该信任Stack Overflow上的答案以获取有关许可证的法律建议.无论如何,对于OP的我想构建一个程序并将其提供给我的朋友"来说,这可能不是问题,但对于以后阅读此问题的其他人来说,可能是一个问题.

One thing to keep in mind with static linking is that it affects how different licenses play together. In particular, if any of the libraries you're using are LGPL-licensed, static-linking them has consequences that dynamic linking doesn't. See this question (and the links on the answer) for more details, but really, you shouldn't trust answers on Stack Overflow for legal advice on licenses. Anyway, that probably isn't an issue for the OP's "I want to build a program and give it to my friend", but for others reading this question later, it might be.

如果不可能或不希望进行静态链接,那么您所需要做的就是将可执行文件和dylib打包在一起,然后将其放入您朋友的计算机上.由于他显然不想运行安装程序,因此这意味着tarball或zipfile.

If static linking isn't possible or desirable, all you need to do is package up the executable and dylibs together and get them onto your friend's machine. Since he apparently doesn't want to run an installer, that means a tarball or zipfile.

唯一棘手的部分是确保exe知道在哪里找到dylib.除非每个dylib都位于dyld搜索路径中(有关更多详细信息,请参见dyld的联机帮助页,但如果您不想安装任何东西,它不会对您有帮助),或者位于您当时的位置链接它,运行可执行文件将因dyld出现找不到图像"错误而失败.

The only tricky part is making sure the exe knows where to find the dylibs. Unless each dylib is either in the dyld search path (see the manpage for dyld for more details, but it's not going to help you if you don't want to install anything), or in the exact same place it was at the time you linked it, running the executable will fail with an "image not found" error from dyld.

幸运的是,完全相同的位置"可能意味着"@ executable_path/libfoo.dylib"之类的魔术路径,这意味着与myexe位于同一目录中",而不是诸如"/opt/local/lib/"这样的绝对路径libfoo.dylib"或相对路径,例如"../../foo/build/Release/libfoo.dylib". (请注意,正常的相对路径是相对于当前工作目录的,而不是相对于可执行文件或捆绑软件目录的.)

Fortunately, "exact same place" can mean a magic path like "@executable_path/libfoo.dylib", which means "in the same directory as myexe", rather than an absolute path like "/opt/local/lib/libfoo.dylib" or a relative path like "../../foo/build/Release/libfoo.dylib". (Note that normal relative paths are relative to the current working directory, not the executable or bundle directory.)

您可以通过以下操作查看myexe的位置:

You can see where myexe is looking by doing this:

otool -L myexe

@executable_path中找不到的所有内容(/lib和/usr/lib中的内容除外,它们是操作系统的一部分,不需要分发),您可以像这样进行修复:

Anything that's not looking in @executable_path (except stuff in /lib and /usr/lib, which is part of the OS and doesn't need to be distributed), you can fix like this:

install_name_tool -change ../../../mydl/build/Release/libmydl.dylib @executable_path/libmydl.dylib myexe

现在,您只需要复制myexe旁边的所有dylib,将其压缩,然后提供给您的朋友,他就可以解压缩并运行exe.

Now, you just need to copy all those dylibs right next to myexe, tar it up, and give it to your friend, and he can just untar it and run the exe.

这篇关于我可以在我的可执行文件中包含dylib -s吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆