c - 执行二进制文件时获取 "exec format error"

标签 c gcc

#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/

相关文章:

c - 对 'clrscr' 的 undefined reference 和 ld 返回 1 退出状态

c++ - C++ 代码中的 volatile 相关错误

c - 为什么 -fpie 在裸机代码中不起作用并导致野指针?

gcc - 将结构体偏移属性赋予汇编器

gcc - arm-linux-gcc 和 arm-none-linux-gnueabi 有什么区别

c - 矩阵中的排序列(比较词典编排) | C

c - 如何为 PIC18 制作定时器?

c - 错误 C2143 : syntax error : missing ';' before 'type' when transferring pointer

c - Raw Sockets 与 Libpcap 在发送性能方面的对比

c - GCC 内联汇编操作所有寄存器?