【问题标题】:g++ compiling errors in ubuntuubuntu中的g ++编译错误
【发布时间】:2012-09-30 09:25:37
【问题描述】:

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

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 文件。我不完全确定是什么问题。

【问题讨论】:

    标签: c++ gcc ubuntu permissions g++


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      首先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 上存在权限问题

      【讨论】:

      • 尝试将其作为 sudo 运行,但运气不佳,甚至将所有目录的 chmod 设置为 777。
      • 您不必对目录进行 chmod,而是对可执行文件进行 chmod。我仍然对你的情况感到困惑,让我考虑一下。
      • 好的,最后一次尝试,我不确定你是否需要安装 build-essential 包。试试 sudo apt-get install build-essential
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多