如何找出给定对象所在的库?
我正在运行 Irix 6.5 的 SGI 上使用 FORTRAN 和 C 进行编程,但这应该适用于所有类 Unix 系统。 当出现“无法解析的文本符号”链接错误时,如何找到需要链接到程序的库? 这是我从链接器中看到的示例:
ld32: ERROR 33 Unresolved text symbol "ortho2_" -- first referenced by ./libfoo.a
我是否只需要知道需要哪些库,或者是否有一些工具或命令可以帮助我解决这个问题?
I'm programming in FORTRAN and C on an SGI running Irix 6.5, but this should be applicable to all Unix-like systems. How do I find which library I need to link to my program when I get an "unresolved text symbol" link error? Here's an example of what I'm seeing from the linker:
ld32: ERROR 33 Unresolved text symbol "ortho2_" -- first referenced by ./libfoo.a
Do I just have to know which libraries are required, or is there some tool or command that can help me figure this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 nm 命令列出共享库中的动态符号:
然后使用 grep 命令查找您要查找的符号。 对于静态库,省略 -D。 您可以在循环中使用它或与 xargs 一起使用它来扫描多个库。
我通常只使用谷歌(假设该符号来自公开可用的库)。
You can use the
nm
command to list the dynamic symbols from a shared library:and then
grep
for the symbol you are looking for. Omit the -D for static libraries. You can use this in a loop or with xargs to scan multiple libraries.I usually just use google (assuming the symbol is from a publicly available library).
使用
nm
(如 罗伯特·甘布尔的回答)是您问题的正确答案。 诀窍在于知道在哪里寻找图书馆。 你的程序是做什么的? 如果存在大量数字,您很可能应该链接到数学库(例如 LAPACK 或 BLAS),并且可能想要开始在那里查找。 Web 搜索也很有帮助 - 我在我最喜欢的搜索引擎中输入“ortho2”并得到 此文档表明它位于libfgl.a
请注意,当您搜索时,您可能应该省略尾部下划线 - 它通常会添加到例程中由编译器命名。
Using
nm
(as in Robert Gamble's answer) is the correct answer to your question. The trick is in knowing where to look for the libraries. What does your program do? If there is a large amount of numerics going on, chances are you should be linking against math libraries (like LAPACK or BLAS) and might want to start looking there. Web searching can also be helpful - I typed "ortho2" into my favorite search engine and got this documentation that suggests it's inlibfgl.a
Note that when you search you should probably omit the trailing underscore - it's usually added to the routine name by the compiler.
我抵制住了将其添加到罗伯特·甘布尔的答案中的诱惑——将其视为对其的补充。
请警惕简单地假设“任何匹配”都可以使用。 在另一个 SO 问题中存在一个情况,其中一段代码从 Windows 移动到 Unix,并且 Windows 代码使用
getch()
从输入中读取单个字符。 用户经历了与此类似的过程,并在 Unix 上的curses
库中找到了getch()
。 因此,用户链接到curses库并想知道为什么代码核心被转储。 问题是,实际使用的 getch() 假设已经完成了正确的初始化,而正确的初始化尚未完成。 事实上,这可能不是我们所需要的例行公事。在 Solaris 上,
nm
有一些选项可以告诉您库名称,甚至是库中包含该符号的目标文件(-r
表示库名称,< code>-R 用于库内的对象)。也要小心 C++ 损坏的名称。
ortho2
示例显然不是 C++ 损坏的。I resisted the temptation to add this to Robert Gamble's answer - consider this a supplement to it.
Be wary of simply assuming that 'any match' is OK for use. There was a case in another SO question where a piece of code was moved from Windows to Unix and the Windows code used
getch()
to read a single character from the input. The user went through a process analogous to this and foundgetch()
on Unix in thecurses
library. So, the user linked with thecurses
library and wondered why the code core dumped. The trouble is, thegetch()
actually used assumes that the proper initialization has been done, and the proper initialization was not done. In fact, it probably wasn't the routine that was needed.On Solaris, there are options to
nm
that tell you the library name and even object file within the library that contains the symbol (these are-r
for library name, and-R
for object within library).Beware C++ mangled names, too. The
ortho2
example is clearly not C++ mangled.如果它是标准函数(ortho2 可能是),则手册页会告诉您它位于哪个库中。
If it's a standard function, which ortho2 could be, the man page will tell you which library it is in.
我有一个小的查找脚本。 输入查找 func_name
I have a little lookfor script. Enter lookfor func_name