问题描述
我有一个Makefile for linux,我正在移植到达尔文。 makefile接收一堆.o文件,并将它们链接到一个.so共享对象。好吧,所以我想(我错了这个?),在达尔文最好的模拟是dylib。所以我把-shared标志更改为-dynamiclib。
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.
现在,我链接到dylib的代码取决于很多外部库。当我尝试构建dylib,我得到错误说有未定义的引用。但是Linux Makefile不会在创建.so文件的构建步骤中指定任何-lwhatever或-L / path / whatever选项。 Hm?这是因为当您创建ELF .so文件时,默认情况下会留下外部引用未解析,然后加载共享库时,会递归加载共享库,这些共享库依赖于共享库正在加载?不是这样的情况下,如果共享库依赖于.a或.o文件,你应该静态链接到共享库,否则你不能在运行时链接?除非引用也是动态可加载的库,否则在运行时加载的库中有未定义的引用,你怎么能逃避?
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
b $ b
它不需要我在创建共享库时添加那些-l和-L选项。但我仍然不明白这是如何工作的。
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.
推荐答案
还讨论此问题。我认为关键点是,为了获得像Linux的链接行为,你需要指定-undefined dynamic_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.
这篇关于在共享库或dylib中处理未定义的符号是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!