【问题标题】:How can I resolve foo's library path using g++ -Lhere -Lthere -lfoo without building an object?如何在不构建对象的情况下使用 g++ -Lhere -Lthere -lfoo 解析 foo 的库路径?
【发布时间】:2010-07-08 17:05:25
【问题描述】:

我想显示将在编译的链接阶段使用的库的显式路径。我想这样做,以便我可以将库作为依赖项添加到单独的目标文件中。

换句话说,我经常使用:

g++ myFile.cpp -Lsomewhere -Lelse -Lhere -Lthere -lfoo

有没有办法强制 g++、ld、ldd 或其他东西使用 -L 来解析“-lfoo”而不实际链接任何东西,以便我可以使用显式路径作为依赖项?如需更明确的信息,请参阅Makefile Updated Library Dependency

【问题讨论】:

    标签: c++ makefile linker resolution


    【解决方案1】:

    这并不理想,我希望有一个更清晰的答案,但是您可以从 gcc 获取默认搜索路径,然后在每个路径中搜索文件。它在 GNU make 中:

    libnams = foo bar
    dirs = somewhere else here there
    dirs += $(subst :, ,$(subst =,,$(word 2,$(shell gcc -print-search-dirs | grep libraries))))
    exts = a so
    paths = $(foreach L, $(libnams), \
               $(firstword $(foreach D, $(dirs), \
                  $(foreach E, $(exts), \
                     $(wildcard $(D)/lib$(L).$(E))))))
    

    【讨论】:

      【解决方案2】:

      由于您知道链接器搜索目录以查找相关库 (see the manual) 的顺序,因此您可以使用 Make 的 vpath 以相同的顺序搜索它们:

      vpath %.so somewhere else here there
      
      otherObjectFile: foo.so
        #whatever...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-15
        • 2015-05-11
        • 2012-08-27
        • 1970-01-01
        • 2016-12-28
        相关资源
        最近更新 更多