Linux gcc 编译的二进制文件给出“无法执行二进制文件”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3740322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:12:09  来源:igfitidea点击:

gcc compiled binaries give "cannot execute binary file"

linuxgccpermissionsbinary

提问by Nathan Fig

I compile this program:

我编译这个程序:

#include <stdio.h>

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

With this command:

使用此命令:

  gcc -c "hello.c" -o hello

And when I try to execute hello, I get

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

bash: ./hello: Permission denied

Because the permissions are

因为权限是

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

For some reason??

因为某些原因??

But whatever... after changing the permissions and trying to execute again, I get

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

bash: ./hello: cannot execute binary file

I'm using gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

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

What am I doing wrong here? It's gotta be obvious... it's just too late for me to keep using my tired eyes to try and figure out this simple problem....

我在这里做错了什么?这一定是显而易见的……对我来说,继续用我疲惫的眼睛试图找出这个简单的问题已经太晚了……

P.S. I do (sometimes) work on programs more sophisticated than Hello World, but gcc is doing this across the board...

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

采纳答案by kwatford

Take the -cout. That's for making object files, not executables.

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

回答by eldarerathis

The -cflag tells it not to link, so you have an object file, not a binary executable.

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

In fact, if you ran this without the -oflag, you would find that the default output file would be hello.o.

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

For reference (and giggles), the man entry on the -cflag:

作为参考(和咯咯笑),-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.

回答by karlphillip

Compile with: gcc hello.c -o hello

编译: gcc hello.c -o hello