x86_64“gcc -S” ->作为-> ld->执行失败
我正在尝试通过“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
-dynamic-linker /lib/ld-linux-x86-64.so.2
而不是/lib64/ld-linux.so.2
进行链接,这是对于 32 位。总而言之,这对我有用:
如果仍然不起作用,请按照@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.All in all, this works for me:
If that still doesn't work follow the suggestion of @n.m. and check the output of
gcc -v -o ctest ctest.c
.-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.