很容易让程序在编译时找出依赖关系(使用gcc -MM)。然而,链接依赖(决定应链接哪些库)似乎很难弄清楚。当需要链接到各个库的多个目标时,这个问题就会出现。
例如,需要构建三个动态库目标t1.so,t2.so和t3.so。 t1.so需要数学库(-lm),而t2和t3则不需要。编写单独的规则会很繁琐。需要与数学库链接的三个目标的单个规则可以省去麻烦。但是,由于数学库未用于t2.so和t3.so。
,因此会导致目标大小膨胀有什么想法吗?
答案 0 :(得分:1)
这并不像找到所需的标题那样容易理解。 gcc -MM
只是使用预处理器的一种奇特方式,但它几乎不了解代码的使用方式或工作方式:您可以包含一些标题为#define
的文件或引入复杂的依赖项库的依赖关系。
我会坚持为所有目标编写显式链接依赖项(在您的情况下为3)。您可以在LDFLAGS
中收集常见的依赖项。
答案 1 :(得分:1)
看起来ld
的{{1}}选项是一个好的开始。输出需要格式化,但我认为它包含所有正确的信息。
我的调用看起来像这样:
--trace
答案 2 :(得分:0)
你尝试过使用'nm'吗?它为您提供了对象/库文件中已定义和未定义符号的列表(请参阅文档here。
Bernd Strieder在post中提到了一种我正考虑使用的方法 -
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.