简体   繁体   English

Makefile自动链接依赖?

[英]Makefile automatic link dependency?

It's easy to let program figure out the dependency at compile time, (with gcc -MM). 让程序在编译时找出依赖关系很容易(使用gcc -MM)。 Nevertheless, link dependency (deciding which libraries should be linked to) seems to be difficult to figure out. 然而,链接依赖(决定应链接哪些库)似乎很难弄清楚。 This issue become emergent when multiple targets with individual libraries to link to are needed. 当需要具有要链接的单个库的多个目标时,此问题变得紧急。

For instance, three dynamic library targets t1.so, t2.so and t3.so needs to be built. 例如,需要构建三个动态库目标t1.so,t2.so和t3.so。 t1.so needs math library (-lm), while t2 and t3 don't. t1.so需要数学库(-lm),而t2和t3则不需要。 It would be tedious to write separate rules. 编写单独的规则会很繁琐。 A single rule requiring the three targets linked with math library saves the trouble. 需要与数学库链接的三个目标的单个规则可以省去麻烦。 However, it causes inflation of target size since math library is unused for t2.so and t3.so. 但是,由于数学库未用于t2.so和t3.so,因此会导致目标大小膨胀。

Any ideas? 有任何想法吗?

It looks like ld 's --trace option is a good start. 看起来ld--trace选项是一个好的开始。 The output needs formatting, but I think it contains all the right information. 输出需要格式化,但我认为它包含所有正确的信息。

My invocation looks something like this: 我的调用看起来像这样:

$ 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

This is not as easy to figure out as finding needed headers. 这并不像找到所需的标题那么容易理解。 gcc -MM is just some fancy way to use the preprocessor, but it knows pretty much nothing about the way the code is used or works: you could include some headers full of #define 's or introduce complex dependencies library dependencies. gcc -MM只是使用预处理器的一种奇特方式,但它几乎不知道代码的使用方式或工作原理:你可以包含一些充满#define的头文件或引入复杂的依赖库相关性。

I would stick with writing explicit linking dependencies for all targets (3 in your case). 我会坚持为所有目标编写显式链接依赖项(在您的情况下为3)。 You can collect common dependencies in LDFLAGS . 您可以在LDFLAGS收集常见的依赖项。

Have you tried using 'nm'? 你尝试过使用'nm'吗? It gives you a list of defined and undefined symbols in object/library files (see documentation here . 它为您提供了对象/库文件中已定义和未定义符号的列表(请参阅此处的文档。

There's an approach mentioned in this post by Bernd Strieder that I am considering using - 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