简体   繁体   中英

Makefile automatic link dependency?

It's easy to let program figure out the dependency at compile time, (with 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 needs math library (-lm), while t2 and t3 don't. 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.

Any ideas?

It looks like ld 's --trace option is a good start. 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.

I would stick with writing explicit linking dependencies for all targets (3 in your case). You can collect common dependencies in LDFLAGS .

Have you tried using '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 -

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM