繁体   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