【发布时间】:2015-07-13 04:03:09
【问题描述】:
我记得在互联网上的某个地方确切地阅读过这个标志是什么,但现在我忘记了。
我正在编写一个共享库,我希望我的库允许未定义的引用。这样,无论谁使用我的库,都必须链接到它和链接到它的依赖项。 g++ 上的哪个标志允许这样做?或者它可能是一个链接器标志?
【问题讨论】:
标签: c++ shared-libraries dylib
我记得在互联网上的某个地方确切地阅读过这个标志是什么,但现在我忘记了。
我正在编写一个共享库,我希望我的库允许未定义的引用。这样,无论谁使用我的库,都必须链接到它和链接到它的依赖项。 g++ 上的哪个标志允许这样做?或者它可能是一个链接器标志?
【问题讨论】:
标签: c++ shared-libraries dylib
这只能是一个链接器标志...然后,这取决于您使用的链接器。
在 Linux(可能是 BSD,至少 FreeBSD)上,这应该是默认行为。不过在 Mac 上不行。
--allow-shlib-undefined
--no-allow-shlib-undefined
Allows or disallows undefined symbols in shared libraries.
This switch is similar to --no-undefined except that it determines
the behaviour when the undefined symbols are in a shared library rather
than a regular object file. It does not affect how undefined symbols in
regular object files are handled.
The default behaviour is to report errors for any undefined symbols
referenced in shared libraries if the linker is being used to create an
executable, but to allow them if the linker is being used to create a
shared library.
-undefined treatment
Specifies how undefined symbols are to be treated. Options
are: error, warning, suppress, or dynamic_lookup. The
default is error.
【讨论】:
不要将您的共享库与其依赖库链接,仅此而已。
默认情况下,链接共享库时,允许未定义的引用。现在,如果您将共享库与其依赖项显式链接起来,运行时加载程序将自动加载这些依赖项。如果您没有将共享库与其依赖项显式链接,则与共享库链接将需要与其依赖项链接,以取消解析所有未定义的引用。
【讨论】: