“文件无法识别” 使用 GNU 链接器时
作为新手,我可能做错了什么。 你能帮我一下吗?
我用 C 语言编写了一个名为 hello.c 的简单 Hello World 程序,并运行了以下命令:
gcc -S hello.c
生成了 hello.s
。 然后我将这个文件与 GNU 汇编器 as
一起使用:
as hello.s
它生成了一个不可执行的 a.out
,它仍然需要链接,我明白吗?
我尝试使用 ld 来链接它,如下所示:
ld a.out
但出现以下错误:
a.out: file not recognized: File truncated
并且 ld 删除了我的文件。
这是 x86 Ubuntu 系统。 我究竟做错了什么? 非常感谢!
I'm probably doing something wrong, being a newbie. Could you please help me out?
I've written a simple Hello World program in C called hello.c, and ran the following command:
gcc -S hello.c
That produced hello.s
. Then I used that file with GNU assembler, as
:
as hello.s
Which produced a non-executable a.out
, which still needs to be linked, I understand?
I try to link it by using ld
, like so:
ld a.out
But get the following error:
a.out: file not recognized: File truncated
And ld
deletes my file.
This is an x86 Ubuntu system. What am I doing wrong? Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的第一个问题是:你为什么要汇编代码? 如果您想要汇编代码,请务必使用 gcc -S 来获取它(我猜是为了查看)。
但是您不需要通过
as
运行它来继续,只需使用:第一步将 C 源代码直接转换为可执行文件,第二步将为您提供汇编程序源代码。
您的具体问题可能是
ld
尝试将其输出写入a.out
。 如果这也是您的输入文件,那么它很可能在运行 ld 的过程中被破坏。 您可以在运行 ld 命令:ld a.in
之前尝试将a.out
重命名为a.in
。My first question would be: why are you assembling the code? If you want the assembler code the, by all means, use
gcc -S
to get it (for viewing, I guess).But you don't need to run that through
as
to keep going, just use:That first step will turn the C source directly into an executable, the second will give you your assembler source.
Your specific problem may be that
ld
tries to write its output toa.out
. If that's also your input file, it may well be being destroyed in the process of runningld
. You could try renaminga.out
toa.in
before running the ld command:ld a.in
.我是这样做的:
为什么我调用
gcc
而不是ld
? 因为 GCC 负责链接 C 运行时,并执行其他依赖于实现的操作。 如果您想看到这一点,请使用--verbose
选项:Here is how I do it:
Why do I invoke
gcc
instead ofld
? Because GCC takes care of linking the C runtime, and doing other implementation-dependant stuff. If you want to see that, use the--verbose
option:编辑:好的,我在我的系统上尝试了所有这些,我想我知道问题是什么。
ld
正在写入a.out
(其默认输出文件),同时从中读取。 尝试这样的事情:EDIT: okay, I tried this all out on my system, and I think I know what the problem is.
ld
is writing toa.out
(its default output file), while reading from it at the same time. Try something like this:无论如何,重新安装 glibc-devel 并检查它是否有效。 这个过程对我有用。
reinstall glibc-devel anyway you can and check if it work. this process work for me.