Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My C program fails to run with `cannot execute binary file: Exec format error`

Tags:

c

I'm just starting with C. I'm trying to compile the below code and execute it, but I get an error.

Also running size shows nothing in BS or data stacks?

#include<stdio.h>
/* test.c:  My first C program on a Linux */
int main(void)
{
 printf("Hello! This is a test prgoram.\n");
 return 0;
}

Compiling works:

gcc -c test.c -o test

Executing:

bash: ./test: cannot execute binary file: Exec format error

Size:

  text     data     bss     dec     hex filename
    108       0       0     108      6c test
like image 940
Ankh2054 Avatar asked Jan 07 '16 11:01

Ankh2054


2 Answers

Ok since no one wants wanted to say why... you need to remove the -c option because according to the man:

the -c option says not to run the linker. Then the output consists of object files output by the assembler.

So basically, you are not creating a fully executable file, just object files.

Here's some quick info on how compiling works: http://courses.cms.caltech.edu/cs11/material/c/mike/misc/compiling_c.html

like image 131
Paulo Bu Avatar answered Sep 21 '22 04:09

Paulo Bu


The -c flag is not correct in this case:

gcc test.c -o test

-c This option is used to compile the program.

like image 45
Michał Szydłowski Avatar answered Sep 22 '22 04:09

Michał Szydłowski