2012-01-19 18 views
1

我想靜態編譯pdf2svg,這樣我就可以在穩定的Debian中使用最新的版本。 ./configure不給--enable-static選項,所以我手動添加鏈接器的Makefile-static選項。如何讓gcc/ld在使用-static時迭代許多'-l library'?

不幸的是,結果並不像我懷疑的那樣。鏈接給了我大量的undefined reference錯誤。經過一些Google搜索後,我發現問題是由於-lsome_lib的錯誤訂單造成的。 Gcc鏈接程序試圖在每個庫中靜態鏈接一次,當它第一次看到它時 - info和Stackoverflow問題:Why does the order in which libraries are linked sometimes cause errors in GCC?

是否有可能讓連接器在庫列表中多次傳遞?

回答

8

也許這是你的搜索內容(來自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. 
+0

這是問題的答案,但遺憾的是它並沒有幫我。我結束了使用庫和LD_LIBRARY_PATH。 – Yax

0

Tick是,只要有可能,靜態引用到的類的對象(或功能),其沒有鏈接到同一個庫的另一個cpp文件中(或者在另一個已經使用的庫中)。

我有這種情況:

  • 庫中的帶班裏昂證券clsA.cpp給出錯誤
  • 庫A和Foo.cpp中,讓沒有引用錯誤
  • 庫B中使用類里昂證券
  • 應用程序使用這兩個庫,並使用類/功能從Foo.cpp中

我得到的尚未解決的指在Application中使用clsA類的庫B中的對象。

鏈接應用程序與庫A和B給我的錯誤。由於我使用CodeLite,因此很難更改庫命令。我只是把一個靜態的對象Foo.cpp中:

#include "clsA.h" 
clsA objA; 

鏈接器現在看到的是里昂證券在庫中的引用(Foo.cpp中之間),將在應用程序中正確鏈接,因爲Foo.cpp中已經聯繫在一起的。

但即使對象是在一個虛擬的功能,從來沒有所謂的創造,所以對象將永遠不會被分配的伎倆工作:

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