共享库或 dylib 中未定义的符号是怎么回事?
我有一个 Linux 的 Makefile,我正在将其移植到 Darwin。 makefile 接受一堆 .o 文件并将它们链接在一起形成一个 .so 共享对象。好吧,所以我想(我对此有错吗?)达尔文最好的模拟是 dylib。所以我将 -shared 标志更改为 -dynamiclib。
现在,我链接到 dylib 中的代码依赖于许多外部库。当我尝试构建 dylib 时,出现错误,提示存在未定义的引用。但 Linux Makefile 在创建 .so 文件的构建步骤中未指定任何 -lwhatever 或 -L/path/whatever 选项。嗯?这是因为当您创建 ELF .so 文件时,默认情况下它会保留未解析的外部引用,然后在加载共享库时,它会递归地加载您所依赖的共享库正在加载?如果共享库依赖于 .a 或 .o 文件,您是否必须将它们静态链接到共享库中,否则您将无法在运行时链接?除非引用也是动态可加载库,否则如何避免在运行时加载的库中存在未定义的引用?
无论如何,如果我指定
-undefined suppress -flat_namespace
它不需要我在创建共享库时添加这些 -l 和 -L 选项。但我仍然不明白这最终是如何运作的。
I have a Makefile for linux that I am porting over to Darwin. The makefile takes a bunch of .o files and links them together into a .so shared object. Okay, so I figured (am I wrong about this?) that the best analog for this in Darwin is the dylib. So I changed the -shared flag to -dynamiclib.
Now the code that I am linking together into the dylib depends on lots of external libraries. When I try to build the dylib, I get errors saying there are undefined references. But the Linux Makefile does not specify any of the -lwhatever or -L/path/whatever options in the build step that creates the .so file. Hm? Is this because when you create an ELF .so file, by default it leaves external references unresolved, and then when the shared library is loaded, it recursively loads shared libraries which are depended on by the shared library you are loading? Wouldn't it be the case that if the shared library depends on a .a or .o file, you would HAVE to statically link them into the shared library, otherwise you could not link at runtime? How can you get away with having undefined references in a library that is loaded at runtime, unless the references are also to dynamically loadable libraries?
Anyway so if I specify
-undefined suppress -flat_namespace
it doesn't require me to add those -l and -L options when creating the shared library. But I still don't understand how this can work ultimately.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此帖子也讨论了这个问题。我认为关键点在于,为了获得类似 Linux 的链接行为,您需要指定“-undefineddynamic_lookup”标志。默认情况下,如果动态库中存在任何未定义的引用,Darwin 链接器会抛出错误。您还可以使用 -U 在每个符号的基础上设置此行为。请参阅“man ld”以供参考。
This thread also discusses this issue. I think the key point is that in order to get the Linux-like linking behavior, you need to specify the "-undefined dynamic_lookup" flag. By default, the Darwin linker throws an error if there are any undefined references in a dynamic library. You can also use -U to set this behavior on a per-symbol basis. See 'man ld' for reference.
使用
libtool
。Use
libtool
.