【问题标题】:gcc compiled binaries give "cannot execute binary file"gcc 编译的二进制文件给出“无法执行二进制文件”
【发布时间】:2011-04-14 00:14:10
【问题描述】:

我编译这个程序:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}

使用这个命令:

  gcc -c "hello.c" -o hello

当我尝试执行 hello 时,我得到了

bash: ./hello: Permission denied

因为权限是

-rw-r--r-- 1 nathan nathan   856 2010-09-17 23:49 hello

出于某种原因??

但是无论如何...在更改权限并尝试再次执行后,我得到了

bash: ./hello: cannot execute binary file

我正在使用 gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

我在这里做错了什么?这一定很明显……我用疲倦的眼睛试图弄清楚这个简单的问题已经太晚了……

附:我确实(有时)在比 Hello World 更复杂的程序上工作,但 gcc 正在全面这样做......

【问题讨论】:

  • 执行:file hello 并将结果粘贴到此处。你应该编译: gcc hello.c -o hello
  • 总是询问所有警告和调试信息(例如gcc -Wall -g hello.c -o hello

标签: linux gcc permissions binary


【解决方案1】:

取出-c。那是为了制作目标文件,而不是可执行文件。

【讨论】:

  • 补充一点,您制作的hello 文件是一个目标文件(如果这不是很明显的话)。如果您使用-c 选项构建,则应将其命名为hello.o
【解决方案2】:

-c 标志告诉它不要链接,所以你有一个目标文件,而不是二进制可执行文件。

事实上,如果你在没有-o 标志的情况下运行它,你会发现默认输出文件是hello.o

作为参考(和咯咯笑),-c 标志上的 man 条目:

-c  Compile or assemble the source files, but do not link.  The linking stage simply is not done.
    The ultimate output is in the form of an object file for each source file.

    By default, the object file name for a source file is made by replacing the suffix .c, .i, .s,
    etc., with .o.

    Unrecognized input files, not requiring compilation or assembly, are ignored.

【讨论】:

    【解决方案3】:

    编译:gcc hello.c -o hello

    【讨论】:

      猜你喜欢
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多