簡體   English   中英

使用-static時,如何使gcc / ld遍歷許多“ -l庫”?

[英]How to make gcc/ld iterate over many '-l library' when using -static?

我想靜態編譯pdf2svg這樣我就可以在穩定的Debian中使用最新版本。 ./configure不提供--enable-static選項,因此我在鏈接器的Makefile -static選項中手動添加了。

不幸的是結果並不像我所懷疑的那樣。 鏈接給了我大量undefined reference錯誤。 經過一番谷歌搜索后,我發現問題是由-lsome_lib的錯誤順序引起的。 Gcc鏈接器在第一次看到每個庫時會嘗試一次靜態鏈接- 信息和Stackoverflow問題: 為什么庫的鏈接順序有時會在GCC中引起錯誤?

是否有可能使鏈接程序多次通過庫列表?

也許這就是您要搜索的內容(來自gnu ld手冊頁):

   -( archives -)
   --start-group archives --end-group
       The archives should be a list of archive files.  They may be either explicit file names, or -l options.

       The specified archives are searched repeatedly until no new undefined references are created.  Normally, an archive is searched only once in the order that it is
       specified on the command line.  If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on
       the command line, the linker would not be able to resolve that reference.  By grouping the archives, they all be searched repeatedly until all possible references
       are resolved.

       Using this option has a significant performance cost.  It is best to use it only when there are unavoidable circular references between two or more archives.

勾號是在可能的情況下,為未在相同庫的另一個cpp文件(或已使用的另一個庫)中鏈接的類的對象(或函數)添加靜態引用。

我有這種情況:

  • clsA.cpp中具有類clsA的庫A給出錯誤
  • 帶有foo.cpp的庫A,沒有給出參考錯誤
  • 使用類clsA的庫B
  • 應用程序同時使用兩個庫並使用foo.cpp中的類/函數

使用庫B中使用clsA類的對象時,我在應用程序中獲得了未解決的引用。

將應用程序與庫A和B鏈接會給我錯誤。 由於我使用CodeLite,因此很難更改庫順序。 我只是將一個靜態對象放在foo.cpp中:

#include "clsA.h"
clsA objA;

鏈接器現在看到在庫A中(在foo.cpp之間)引用了clsA,並且將在應用程序中正確鏈接,因為foo.cpp已被鏈接。

但是,即使對象是在偽函數中創建的,但從未調用過,該技巧仍然有效,因此永遠不會分配該對象:

// foo.cpp
#include "clsA.h"
void dummyf()
{
   clsA objA;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM