如何使用 g++ 解析 foo 的库路径-Lhere -Lthere -lfoo 不构建对象?

发布于 2024-09-08 12:50:54 字数 362 浏览 8 评论 0原文

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

换句话说,我经常使用以下链接进行链接:

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

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

I would like to display the explicit path to a library which will be used in the linking stage of compilation. I want to do this so that I can add the library as a dependency to a separate object file.

In other words, I often link using:

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

Is there a way to coerce g++, ld, ldd, or something else to resolve '-lfoo' using the -L's without actually linking anything so that I can use the explicit path as a dependency? For more explicit info, see Makefile Updated Library Dependency.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-09-15 12:50:54

这并不理想,我希望有一个更清晰的答案,但您可以从 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))))))

This isn't ideal, and I hope there is a cleaner answer, but you can get the default search paths from gcc, then search each one for the files. Here it is in 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))))))
﹏雨一样淡蓝的深情 2024-09-15 12:50:54

因为您知道链接器搜索目录以查找相关库的顺序(参见手册),你可以使用Make的vpath以相同的顺序搜索它们:

vpath %.so somewhere else here there

otherObjectFile: foo.so
  #whatever...

Since you know the order in which the linker searches directories looking for the library in question (see the manual), you can use Make's vpath to search them in the same order:

vpath %.so somewhere else here there

otherObjectFile: foo.so
  #whatever...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文