#include <stdio.h>
#include <stdlib.h>
int main()
{
volatile int x;
printf("without assignment %d", x);
x = 100;
printf("%d", x);
}
<小时/>
gcc -c -o volatandconstvolatile volatandconstvolatile.c
我得到了volatandconstvolatile
文件但是
-rw-rw-r-- 1 naveenkumar naveenkumar 1600 Aug 8 05:12 volatandconstvolatile
然后我更改了权限chmod 777 volatandconstvolatile
然后./volatandconstvolatile
./volatandconstvolatile: cannot execute binary file: Exec format error
objdump volatandconstvolatile | grep "archit"
architecture: i386:x86-64, flags 0x00000011:
readelf -a -W volatandconstvolatile
我知道volatile
用于从外部手段获取值。
为什么我会收到此错误?
最佳答案
gcc -c -o volatandconstvolatile volatandconstvolatile.c
这个编译命令行没有任何意义。
-c
表示“仅编译”并且不链接。但是您保留了 .o
后缀,这意味着您正在构建一个完整的可执行文件,这是错误的。
您可以像这样在一次 GCC 调用中构建完整的应用程序,只需省略 -c
:
gcc -o volatandconstvolatile volatandconstvolatile.c
请注意,LD 自动设置输出二进制文件的可执行位。事实上,您必须手动 chmod
该文件应该是出现问题的线索。
关于c - 执行二进制文件时获取 "exec format error",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51737345/