2015-07-12 47 views
1

我記得在網上某個地方準確地讀到了這個標記,但現在我忘記了。允許共享庫中的未定義引用的g ++標誌是什麼?

我正在寫一個共享庫,我希望我的庫允許未定義的引用。那樣,誰使用我的圖書館必須鏈接它鏈接反對它的依賴。 g ++上的標誌是否允許這樣做?或者,也許它是一個鏈接標誌?

回答

3

這隻能是一個鏈接器標誌...然後,它取決於你使用的鏈接器。
在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. 
+0

啊,你說得對......這篇文章我讀了關於Linux和不支持Mac。雖然謝謝! – Dovahkiin

2

只是不要將您的共享庫與其依賴庫鏈接,就這些了。

默認情況下,鏈接共享庫時,允許使用未定義的引用。現在,如果您明確地將您的共享庫與其依賴關聯起來,那麼運行時加載器將自動加載這些依賴關係。如果你沒有明確地鏈接你的共享庫和它的依賴關係,那麼鏈接你的共享庫將需要鏈接它的依賴關係,以便解除所有未定義的引用。