分享

(原创)gcc ——“cannot execute binary file”

 happy123god 2012-05-03
今天在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.


(此段英语摘自:

也就是说带了-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编译命令是否有问题,
生成的文件是否是你预期的执行文件。



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多