GCC编译的二进制文件出现“无法执行二进制文件”的错误提示。

20

我编译了这个程序:

#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

我在这里做错了什么?很明显...对于我来说太晚了,因为我疲惫不堪地用我的疲倦的眼睛尝试解决这个简单的问题....

P.S. 我有时会处理比Hello World更复杂的程序,但gcc会在所有情况下都遇到此问题...


2
执行命令:file hello 并将结果粘贴在此处。您应该使用以下编译命令:gcc hello.c -o hello。 - karlphillip
始终要求所有警告和调试信息(例如 gcc -Wall -g hello.c -o hello - Basile Starynkevitch
3个回答

29

去掉-c。这个选项是用来生成目标文件的,而不是可执行文件的。


只是补充一下,你正在创建的 hello 文件是一个目标文件(如果这不明显的话)。如果你使用 -c 选项构建,则应将其命名为 hello.o - Starkey

7
-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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接