ld 找不到要链接的库

发布于 2024-09-11 17:13:08 字数 799 浏览 7 评论 0原文

下面举个例子来描述我的问题:

ld -Lpath1 -Lpath2 -lA -lB -Xlinker -T -Xlinker \
    -W1,-rpath,/usr/local/lib -l-o target
ld: cannot find -lA
collect2: ld returned 2 exit status

path1和path2都是相对路径,我根据ld的pwd可以找到库A,那么ld为什么会输出这个错误消息呢?

谁能给我一些建议来调试这个问题?

我想念一些,在名为 rt 的库之前有一个“-static”。

根据您的建议,我尝试让 gcc 驱动 ld 进行链接过程。 gcc 奥博 -mabi=64 -static -lrt -Xlinker -T -Xlinker ld.script -W1,-rpath,/usr/local/lib -lmemdbg -o target 它不起作用。

然后我删除“-static”选项,以及-lpthread之后的另一个动态库(因为rt依赖于当我删除“-static”时发现的pthread)

gcc Ao Bo -mabi=64 -lrt -lpthread -Xlinker - T -Xlinker ld.script -W1,-rpath,/usr/local/lib -lmemdbg -o 目标 这次,对象已成功链接在一起。

然后我尝试通过将“-v”传递给 gcc 来弄清楚为什么“-static”命令不起作用 。出现了一些“-L”选项,并在搜索列表中找到了一个名为 librt.a 的库。

我真的很困惑。 gcc的版本是4.3

The following is an example to describe my problem:

ld -Lpath1 -Lpath2 -lA -lB -Xlinker -T -Xlinker \
    -W1,-rpath,/usr/local/lib -l-o target
ld: cannot find -lA
collect2: ld returned 2 exit status

Both path1 and path2 are relative paths, and I can find the library A according to the ld's pwd, so why did the ld output this error msg?

Could anyone give me some suggestion to debug this problem?

i miss some, there is a "-static" before a library called rt.

As your suggestion, i try to let gcc drive the ld to do linking process.
gcc A.o B.o -mabi=64 -static -lrt -Xlinker -T -Xlinker ld.script -W1,-rpath,/usr/local/lib -lmemdbg -o target
it don't work.

and then i remove the "-static" option, and another dynamic lib after -lpthread(because rt depend on pthread which is found when i remove the "-static")

gcc A.o B.o -mabi=64 -lrt -lpthread -Xlinker -T -Xlinker ld.script -W1,-rpath,/usr/local/lib -lmemdbg -o target
and this time, the objects is linked together successfully.

and then i try to figure out why the "-static" command don't work by passing a "-v" to gcc
. some "-L" option appeared, and do find a lib called librt.a in the search list.

i really confused. the version of gcc is 4.3

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

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

发布评论

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

评论(1

叶落知秋 2024-09-18 17:13:08

有多种问题可能是影响因素:

  • 您要查找什么名称? path1/libA.apath1/libA.so
  • -W1 选项可能应该是 -Wl,但这不能解释链接错误。
  • -lo 选项可能应该是两个带有 -l 选项参数的选项(除非您确实有一个库 lib-oa 或 <代码>lib-o.so)。
  • 您通常至少指定一个您自己设计的目标文件;只有 Lex/Yacc 库(据我所知)为您提供 main() - 并且仅在经典 Unix 而不是 Linux 系统上。
  • 如果库文件存在于您认为存在的位置,那么它们的类型是否正确?也就是说,如果您正在构建 32 位程序,它们是否可以是 64 位库 - 反之亦然?它们适用于您的硬件吗? (通常,我希望链接器会说比“找不到”更合适的内容,但这可能是一个问题。)
  • 您检查过库文件和目录的权限吗?
  • 您最好不直接调用加载程序,而是使用编译器为您调用加载程序吗?我的经验是,编译器比我更了解如何正确调用加载器 - 而且我看到更多的人在直接使用 ld 时比使用编译器时更痛苦地构建共享对象。

There are various issues that could be factors:

  • What name are you looking for? path1/libA.a? path1/libA.so?
  • The -W1 option should probably be -Wl, but that would not account for the link error.
  • The -l-o option should probably be two options with an argument for the -l option (unless you really have a library lib-o.a or lib-o.so).
  • You normally specify at least one object file of your own devising; only the Lex/Yacc libraries (that I know of) provide a main() for you - and that only on classic Unix and not Linux systems.
  • If the library files exist where you think they do, are they the correct type? That is, if you are building a 32-bit program, could they be 64-bit libraries - or vice versa? Are they for your hardware? (Normally, I'd expect the linker to say something more apposite than 'cannot find', but this can be an issue.)
  • Have you checked permissions on the library files and the directories?
  • Would you be better off not invoking the loader directly but using the compiler to invoke the loader for you? My experience is that the compiler knows more about invoking the loader correctly than I do - and I've seen more people come to grief building shared objects when they use ld directly than when using the compiler instead.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文