Getting "exec format error" while executing binary file

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

I got the volatandconstvolatile file but

-rw-rw-r--  1 naveenkumar naveenkumar      1600 Aug  8 05:12 volatandconstvolatile

then I changed the permissions chmod 777 volatandconstvolatile

then ./volatandconstvolatile

./volatandconstvolatile: cannot execute binary file: Exec format error
objdump volatandconstvolatile | grep "archit"
architecture: i386:x86-64, flags 0x00000011:

readelf -a -W  volatandconstvolatile

I know that volatile is used to get the values from external means.

Why do I get this error?

c
gcc
asked on Stack Overflow Aug 8, 2018 by naveen kumar • edited Aug 8, 2018 by Student

1 Answer

3
gcc -c -o volatandconstvolatile volatandconstvolatile.c

This compilation command line makes no sense.

-c means "compile only" and don't link. But you're leaving the .o suffix off, which implies that you're building a complete executable, which is wrong.

You can build your full application in one invocation of GCC like this, by leaving off the -c:

gcc -o volatandconstvolatile volatandconstvolatile.c

Note that LD sets the output binary's executable bit automatically. The fact that you had to chmod the file manually should have been a clue that something wasn't right.

answered on Stack Overflow Aug 8, 2018 by Jonathon Reinhart • edited Aug 8, 2018 by Jonathon Reinhart

User contributions licensed under CC BY-SA 3.0