“文件无法识别” 使用 GNU 链接器时

发布于 2024-07-22 11:07:47 字数 523 浏览 7 评论 0原文

作为新手,我可能做错了什么。 你能帮我一下吗?

我用 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 技术交流群。

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

发布评论

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

评论(4

萌能量女王 2024-07-29 11:07:47

我的第一个问题是:你为什么要汇编代码? 如果您想要汇编代码,请务必使用 gcc -S 来获取它(我猜是为了查看)。

但是您不需要通过 as 运行它来继续,只需使用:

gcc -o hello hello.c
gcc -S hello.c

第一步将 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:

gcc -o hello hello.c
gcc -S hello.c

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 to a.out. If that's also your input file, it may well be being destroyed in the process of running ld. You could try renaming a.out to a.in before running the ld command: ld a.in.

陈甜 2024-07-29 11:07:47

我是这样做的:

> gcc -S forums.c 
> as forums.s -o forums.o
> gcc forums.o -o forums
> ./forums 
test

为什么我调用 gcc 而不是 ld? 因为 GCC 负责链接 C 运行时,并执行其他依赖于实现的操作。 如果您想看到这一点,请使用 --verbose 选项:

> gcc --verbose forums.o -o forums
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++ --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.4.0 (GCC) 
COMPILER_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'forums' '-mtune=generic'
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o forums /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crt1.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crti.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtbegin.o -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../.. forums.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtend.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crtn.o

Here is how I do it:

> gcc -S forums.c 
> as forums.s -o forums.o
> gcc forums.o -o forums
> ./forums 
test

Why do I invoke gcc instead of ld? 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:

> gcc --verbose forums.o -o forums
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --prefix=/usr --enable-shared --enable-languages=c,c++,fortran,objc,obj-c++ --enable-threads=posix --mandir=/usr/share/man --infodir=/usr/share/info --enable-__cxa_atexit --disable-multilib --libdir=/usr/lib --libexecdir=/usr/lib --enable-clocale=gnu --disable-libstdcxx-pch --with-tune=generic
Thread model: posix
gcc version 4.4.0 (GCC) 
COMPILER_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/:/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'forums' '-mtune=generic'
 /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/collect2 --eh-frame-hdr -m elf_i386 --hash-style=both -dynamic-linker /lib/ld-linux.so.2 -o forums /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crt1.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crti.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtbegin.o -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0 -L/usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../.. forums.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/crtend.o /usr/lib/gcc/i686-pc-linux-gnu/4.4.0/../../../crtn.o
小矜持 2024-07-29 11:07:47

编辑:好的,我在我的系统上尝试了所有这些,我想我知道问题是什么。 ld 正在写入 a.out (其默认输出文件),同时从中读取。 尝试这样的事情:

ld a.out -o myprog

EDIT: okay, I tried this all out on my system, and I think I know what the problem is. ld is writing to a.out (its default output file), while reading from it at the same time. Try something like this:

ld a.out -o myprog
夜巴黎 2024-07-29 11:07:47

无论如何,重新安装 glibc-devel 并检查它是否有效。 这个过程对我有用。

reinstall glibc-devel anyway you can and check if it work. this process work for me.

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