2010-03-19 118 views
5

讓程序在編譯時很容易找出依賴關係(使用gcc -MM)。儘管如此,鏈接依賴(決定應該鏈接哪些庫)似乎很難弄清楚。當需要使用單個庫鏈接到多個目標時,此問題就會出現。Makefile自動鏈接依賴關係?

例如,需要建立三個動態庫目標t1.so,t2.so和t3.so。 t1.so需要數學庫(-lm),而t2和t3則不需要。編寫單獨的規則將是單調乏味的。一個規則要求與數學庫鏈接的三個目標節省了麻煩。然而,由於數學圖書館沒有用於t2.so和t3.so,它會導致目標規模的膨脹。

任何想法?

回答

1

這並不像找到需要的標題那樣容易。 gcc -MM只是使用預處理器的一些奇特方式,但它幾乎不知道代碼的使用或工作方式:您可以包含一些完整的#define的頭文件或引入複雜的依賴庫依賴項。

我會堅持爲所有目標(3在你的情況下)寫明確的鏈接依賴關係。您可以收集LDFLAGS中的常見依賴關係。

1

看起來像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 
0

您是否嘗試過使用 '納米'?它給你的對象/庫文件中定義和未定義的符號(見文檔here

有這個post由貝恩德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. 
+0

信息的鏈接是破碎。 – rudolfbyker 2016-12-26 12:42:18