【问题标题】:Makefile automatic link dependency?Makefile自动链接依赖?
【发布时间】:2011-01-29 07:39:35
【问题描述】:

让程序在编译时找出依赖关系很容易(使用 gcc -MM)。然而,链接依赖性(决定应该链接到哪些库)似乎很难弄清楚。当需要多个目标以及要链接到的各个库时,就会出现此问题。

例如,需要构建三个动态库目标 t1.so、t2.so 和 t3.so。 t1.so 需要数学库 (-lm),而 t2 和 t3 不需要。编写单独的规则会很乏味。需要与数学库链接的三个目标的单个规则可以省去麻烦。但是,由于 t2.so 和 t3.so 未使用数学库,因此会导致目标大小膨胀。

有什么想法吗?

【问题讨论】:

    标签: gcc makefile dependencies linkage


    【解决方案1】:

    这并不像找到所需的标题那么容易。 gcc -MM 只是使用预处理器的一种奇特方式,但它对代码的使用或工作方式几乎一无所知:您可以包含一些充满 #define 的标头或引入复杂的依赖库依赖项。

    我会坚持为所有目标(在您的情况下为 3 个)编写显式链接依赖项。可以在LDFLAGS收集常用的依赖。

    【讨论】:

      【解决方案2】:

      看起来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
      

      【讨论】:

        【解决方案3】:

        您是否尝试过使用“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.
        

        【讨论】:

        • 帖子链接已损坏。
        猜你喜欢
        • 2023-04-11
        • 1970-01-01
        • 2012-06-20
        • 2018-04-05
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多