日期:2014-05-16  浏览次数:21146 次

用gcc编译的程序没有执行权限
本帖最后由 coder_jack 于 2013-04-30 16:51:37 编辑
今天突然发现我在ubuntu中用gcc编译一个文件后,生成的可执行文件没有执行权限,就算chmod 777 后也无法执行!求教!
gcc -c pid.c -o pid
 chmod a+x pid
 ./pid
结果:
bash: ./pid: cannot execute binary file
用file查看:
 file pid
结果
pid: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

下面是用file命令查看以前写的一个测试程序,这个是好的。这个以前也是用同样的gcc命令编译的,而且编译的结果本身就有可执行权限。但是用file命令发现和上面的不样
file str-test
结果:
str-test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x86469a8dac9963f16a4986ceec9fbf1f8f689372, not stripped


求解,谢谢
gcc 编译 权限 执行 ubuntu

------解决方案--------------------
楼主用了-c选项,-c选项的含义:
 -c  Compile or assemble the source files, but do not link. 
所以,-o选项输出的pid,实际是一个.o文件,有关键字“relocatable”。
楼主用gcc pid.c -o pid生成的才是可执行文件,“executable”。
------解决方案--------------------
是的,gcc pid.c -o pid