2

每次我尝试使用 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 文件。我不完全确定是什么问题。

4

2 回答 2

5

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

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

于 2012-10-10T00:38:30.080 回答
0

首先,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

于 2012-10-10T05:42:01.440 回答