Ubuntu 中的 G++ 编译错误

g++ compiling errors in ubuntu

本文关键字:错误 编译 G++ 中的 Ubuntu      更新时间:2023-10-16

每次我尝试使用 g++ 在 ubuntu 中编译时,都会收到以下错误

g++ test.cpp -o test
/usr/bin/ld: 1: /usr/bin/ld: /bin: Permission denied
/usr/bin/ld: 2: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 3: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 4: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 5: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 6: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 7: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 8: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 9: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 10: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 11: /usr/bin/ld: test.cpp: not found
/usr/bin/ld: 12: /usr/bin/ld: Syntax error: "(" unexpected

我已经多次删除并重新安装了 g++。/usr/bin 和/usr/bin/ld 的 chmod 是 755,奇怪的是我可以运行它g++ -c test.cpp但是我无法运行 .o 文件。我不完全确定问题是什么。

首先,.o文件不是用来运行的,它应该与其他目标文件(.o)和库(特别是C++和C标准库)链接在一起。但是,我会从您的错误消息中猜测这可能不起作用。

从您的错误消息来看,听起来您可能正在/bin目录中运行此命令。这是不恰当的。您应该在某个您具有写入权限的目录中运行它(例如您的主目录)。此外,它告诉您它找不到您的测试.cpp文件,您确定已cd正确的目录吗?

首先,g++ -c test.cpp只编译或汇编源代码,而不链接。最终输出是一个对象文件。在您的情况下,这是 .o 文件。不能运行 .o 文件。

如上所述g++ -c test.cpp忽略链接部分,这样就不会使用ld,这就是g++ -c test.cpp适合您的原因。

您可以切换到 root 用户并再次运行g++ test.cpp -o test。如果它有效,则您可能在/usr/bin/usr/bin/ld上遇到权限问题