x86_64“gcc -S” ->作为-> ld->执行失败

发布于 2024-11-30 19:32:34 字数 806 浏览 1 评论 0原文

我正在尝试通过“gcc -S”编译一个简化的C源文件-> “作为”-> x86_64 平台上的“ld”。

该过程完成时没有错误,但在执行时,显示“No such file or direcotry”错误消息。

ctest.c
int main()
{
    return 0;
}

> gcc -S ctest.c
> as -o ctest.o ctest.s
> ld -o ctest  /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o
> ./ctest 
bash: ./ctest: No such file or directory

> uname -a
Linux mkb3 2.6.27.48-0.3-default #1 SMP 2010-09-20 11:03:26 -0400 x86_64 x86_64 x86_64 GNU/Linux

我还尝试添加动态链接,如一些谷歌结果中所述。

> ld -o ctest -dynamic-linker /lib64/ld-linux.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

但错误仍然存​​在。

欢迎提出意见和建议。

编辑:我犯了一个错误,/lib64/ld-linux.so.2 在我的 Linux 盒子中不存在。我应该使用/lib64/ld-2.9.so。不知道 ld 不会报告指定不存在的库文件的错误。

I am trying to compile a simplified C source file by "gcc -S" -> "as" -> "ld" on x86_64 platform.

The process finished with no error, but when executed, "No such file or direcotry" error message is shown.

ctest.c
int main()
{
    return 0;
}

> gcc -S ctest.c
> as -o ctest.o ctest.s
> ld -o ctest  /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o
> ./ctest 
bash: ./ctest: No such file or directory

> uname -a
Linux mkb3 2.6.27.48-0.3-default #1 SMP 2010-09-20 11:03:26 -0400 x86_64 x86_64 x86_64 GNU/Linux

I also tried to add dynamic link as stated in some google results.

> ld -o ctest -dynamic-linker /lib64/ld-linux.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

But the error remained.

Comments and suggestions are appreciated.

Edit: I made a mistake the /lib64/ld-linux.so.2 doesn't exist in my linux box. I should have used /lib64/ld-2.9.so. Didn't know that ld doesn't report error with a non-existing library file specified.

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

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

发布评论

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

评论(2

妄断弥空 2024-12-07 19:32:34

您应该使用 -dynamic-linker /lib/ld-linux-x86-64.so.2 而不是 /lib64/ld-linux.so.2 进行链接,这是对于 32 位。

/lib64 $ file -L ld*
ld-linux.so.2:        ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
ld-linux-x86-64.so.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, stripped

总而言之,这对我有用:

$ gcc -S ctest.c
$ as -o ctest.o ctest.s
$ ld -o ctest -dynamic-linker /lib/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

如果仍然不起作用,请按照@nm的建议并检查 gcc -v -o ctest ctest.c 的输出。

You should link using -dynamic-linker /lib/ld-linux-x86-64.so.2 rather than /lib64/ld-linux.so.2 which is for 32-bit.

/lib64 $ file -L ld*
ld-linux.so.2:        ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV), dynamically linked, stripped
ld-linux-x86-64.so.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, stripped

All in all, this works for me:

$ gcc -S ctest.c
$ as -o ctest.o ctest.s
$ ld -o ctest -dynamic-linker /lib/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o ctest.o -lc /usr/lib64/crtn.o

If that still doesn't work follow the suggestion of @n.m. and check the output of gcc -v -o ctest ctest.c.

仅一夜美梦 2024-12-07 19:32:34

-dynamic-linker 对我有用,但我有一个 32 位系统。

运行 gcc -v -o ctest ctest.o 并查看输出的最后一行。尝试将其作为命令运行。如果有效,就开始简化它,扔掉零件,直到它不再有效为止。然后扔掉更多的零件,等等。这就是我如何得到一个有效的命令。

您也可以只使用 gcc -o ctest ctest.o 来代替。

-dynamic-linker works for me, but I have a 32-bit system.

Run gcc -v -o ctest ctest.o and look at the last line of the output. Try to run it as a command. If that works, start to simplify it, throwing out parts until it no longer works. Then throw out some more parts, etc. That's how I arrived to a command that works.

You can also just use gcc -o ctest ctest.o instead.

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