簡體   English   中英

Makefile自動鏈接依賴?

[英]Makefile automatic link dependency?

讓程序在編譯時找出依賴關系很容易(使用gcc -MM)。 然而,鏈接依賴(決定應鏈接哪些庫)似乎很難弄清楚。 當需要具有要鏈接的單個庫的多個目標時,此問題變得緊急。

例如,需要構建三個動態庫目標t1.so,t2.so和t3.so。 t1.so需要數學庫(-lm),而t2和t3則不需要。 編寫單獨的規則會很繁瑣。 需要與數學庫鏈接的三個目標的單個規則可以省去麻煩。 但是,由於數學庫未用於t2.so和t3.so,因此會導致目標大小膨脹。

有任何想法嗎?

看起來ld--trace選項是一個好的開始。 輸出需要格式化,但我認為它包含所有正確的信息。

我的調用看起來像這樣:

$ g++ -o foo a.o b.o -l sfml-graphics -l sfml-window -Wl,--trace
/usr/bin/ld: mode elf_i386
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o
/usr/lib/gcc/i686-linux-gnu/4.6/crtbegin.o
a.o
b.o
-lsfml-graphics (/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/libsfml-graphics.so)
-lsfml-window (/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/libsfml-window.so)
-lstdc++ (/usr/lib/gcc/i686-linux-gnu/4.6/libstdc++.so)
-lm (/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libm.so)
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/lib/i386-linux-gnu/libc.so.6
(/usr/lib/i386-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/i386-linux-gnu/ld-linux.so.2
-lgcc_s (/usr/lib/gcc/i686-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/i686-linux-gnu/4.6/crtend.o
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crtn.o

這並不像找到所需的標題那么容易理解。 gcc -MM只是使用預處理器的一種奇特方式,但它幾乎不知道代碼的使用方式或工作原理:你可以包含一些充滿#define的頭文件或引入復雜的依賴庫相關性。

我會堅持為所有目標編寫顯式鏈接依賴項(在您的情況下為3)。 您可以在LDFLAGS收集常見的依賴項。

你嘗試過使用'nm'嗎? 它為您提供了對象/庫文件中已定義和未定義符號的列表(請參閱此處的文檔。

Bernd Strieder在這篇文章中提到了一種方法,我正在考慮使用 -

1. Use nm to generate a list of symbols in all object/library files involved. 
2. This file is parsed and basically the (U)ndefined and (T)ext symbols 
   and the symbols of main functions are filtered out and mapped to their 
   object files. I found that U and T symbols suffice, which reduces the 
   overall problem considerably compared to the linker, which has to 
   consider all symbols. 
3. The transitive hull of the dependency relation according to U and T 
   symbols between object files is being calculated. 
4. A list of object files needed to resolve all dependencies can be 
   printed for any object file. 
5. For any main object file, a make target to link it is arranged.

暫無
暫無

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

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