gcc ——“cannot execute binary file”

今天在cygwin下面使用gcc编译的时候,用了命令,而非makefile。
命令如下:

gcc -o myapp -c hello.c

结果使用 ./myapp 运行时,提示: “cannot execute binary file” 。
开始我百思不得其解,通过ls查看文件权限,将其mode 改成 0777。
再次执行./myapp,依旧提示 “cannot execute binary file” 。

于是baidu, google, 最终找到了原因:

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

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

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

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.

(此段英语摘自:
http://stackoverflow.com/questions/3740322/gcc-compiled-binaries-give-cannot-execute-binary-file)

也就是说带了-c编译时,-o生成的是.o文件,可以使用file 命令查看文件类型。
file ./myapp

./myapp: 80386 COFF executable not stripped - version 30821

而正常生成的执行程序的类型为 : ( cygwin 安装于windows操作系统下)

./my_app.exe: PE32 executable (console) Intel 80386 (stripped to external PDB), for MS Windows

更改了编译命令,

gcc -o myapp hello.c

./myapp

一切正常。

总结: 如果你遇到了“cannot execute binary file” 这样的提示,还请确认下,你的gcc编译命令是否有问题,
生成的文件是否是你预期的执行文件。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gcc: cannot execute binary file: Exec format error错误提示表明无法执行二进制文件的格式错误。可能的原因有几种,包括: 1. 该二进制文件不是可执行文件,可能是一个库文件或其他类型的文件。 2. 该二进制文件是由不兼容的编译环境编译生成的,比如在另一个操作系统上编译的文件在当前系统上无法执行。 3. 当前用户没有执行该二进制文件的权限。 要解决这个问题,可以考虑以下方法: 1. 确保你正在尝试执行的是一个可执行文件,而不是其他类型的文件。可以使用`file`命令检查文件的类型,比如`file <filename>`。 2. 确保你使用的是相同或兼容的编译环境来编译和执行该二进制文件。如果文件是在另一个操作系统上编译的,可能需要重新编译文件以适应当前系统。 3. 如果你是以非root用户身份尝试执行该文件,请确保你具有执行该文件的权限。可以使用`chmod`命令修改文件的权限,比如`chmod +x <filename>`。 希望这些方法能够帮助你解决gcc: cannot execute binary file: Exec format error错误。如果问题仍然存在,请提供更多详细信息以便我能够提供更准确的帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [解决linux下cannot execute binary file: Exec format error](https://blog.csdn.net/owen7500/article/details/52397353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值