2010-03-19 142 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