gcc编译的二进制文件给出"无法执行二进制文件"

Nat*_*Fig 17 linux permissions binary gcc

我编译这个程序:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用此命令:

  gcc -c "hello.c" -o hello
Run Code Online (Sandbox Code Playgroud)

当我尝试执行你好时,我得到了

bash: ./hello: Permission denied
Run Code Online (Sandbox Code Playgroud)

因为权限是

-rw-r--r-- 1 nathan nathan   856 2010-09-17 23:49 hello
Run Code Online (Sandbox Code Playgroud)

由于某些原因??

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

bash: ./hello: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)

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

我在这做错了什么?这是显而易见的......现在为时已晚,让我疲惫不堪的眼睛试图找出这个简单的问题....

PS我(有时)会做比Hello World更复杂的程序,但是gcc正在全面地做这个......

kwa*_*ord 25

就拿-c出来.这是用于制作目标文件,而不是可执行文件.


eld*_*his 6

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

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

作为参考(和咯咯笑),-c国旗上的男子参赛:

-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.
Run Code Online (Sandbox Code Playgroud)