Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130051
  • 博文数量: 25
  • 博客积分: 290
  • 博客等级: 二等列兵
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-30 23:13
文章分类

全部博文(25)

文章存档

2015年(10)

2014年(7)

2011年(3)

2010年(1)

2008年(4)

我的朋友

分类: C/C++

2014-10-23 15:37:21

  今天在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编译命令是否有问题,
生成的文件是否是你预期的执行文件。

阅读(2468) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~