【问题标题】:What is the g++ flag that permits undefined references in shared libraries?什么是允许共享库中未定义引用的 g++ 标志?
【发布时间】:2015-07-13 04:03:09
【问题描述】:

我记得在互联网上的某个地方确切地阅读过这个标志是什么,但现在我忘记了。

我正在编写一个共享库,我希望我的库允许未定义的引用。这样,无论谁使用我的库,都必须链接到它链接到它的依赖项。 g++ 上的哪个标志允许这样做?或者它可能是一个链接器标志?

【问题讨论】:

    标签: c++ shared-libraries dylib


    【解决方案1】:

    这只能是一个链接器标志...然后,这取决于您使用的链接器。
    在 Linux(可能是 BSD,至少 FreeBSD)上,这应该是默认行为。不过在 Mac 上不行。

    来自GNU ld man page

    --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.
    

    来自Apple ld man page

    -undefined treatment  
             Specifies how undefined symbols are to be treated. Options
             are: error, warning, suppress, or dynamic_lookup.  The
             default is error.
    

    【讨论】:

    • 啊,你是对的......我正在阅读的文章是关于 Linux 而不是 Mac。不过谢谢!
    【解决方案2】:

    不要将您的共享库与其依赖库链接,仅此而已。

    默认情况下,链接共享库时,允许未定义的引用。现在,如果您将共享库与其依赖项显式链接起来,运行时加载程序将自动加载这些依赖项。如果您没有将共享库与其依赖项显式链接,则与共享库链接将需要与其依赖项链接,以取消解析所有未定义的引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 2013-04-15
      • 1970-01-01
      • 2017-05-26
      • 2014-08-22
      • 2013-07-03
      • 1970-01-01
      • 2013-02-01
      相关资源
      最近更新 更多