0

I'm trying to compile c program on my Linux using make utility this what happened if i try to create the .o file

#make size_of.o
cc    -c -o size_of.o size_of.c

compiling process run correctly,but when i execute the executable file i got this error

#./size_of.o
bash: ./size_of.o: cannot execute binary file

Then once again i run make without .o suffix

#make size_of
cc   size_of.o   -o size_of

The compiling and executing process run as i expected.

is there any problem with the program or can you tell me what's wrong? How can i fix this problem and is there any different between executable file in C?

This the program:

#include <stdio.h>

int main (void){
printf("char %d bytes\n",sizeof(char));
printf("short %d bytes\n",sizeof(short));
printf("int %d bytes\n",sizeof(int));
printf("long %d bytes\n",sizeof(long));
printf("float %d bytes\n",sizeof(float));
printf("double %d bytes\n",sizeof(double));
printf("long double %d bytes\n",sizeof(long double));   
return 0;
}

and this is the output:

char 1 bytes
short 2 bytes  
int 4 bytes
long 4 bytes
float 4 bytes
double 8 bytes
long double 12 bytes
0

2 Answers 2

4

.o files are object files, not executables. You have specifically told the compiler to only create object files, because you used the -c flag. You don't run object files, they feed into a linker (along with other things) to create the executable file.

The general (simplified) process is:

Phase
-----
          +---------+
          | main.c  | (source)
          +---------+
               |
Compile........|............................
               |
               V
          +---------+              +-----------+
          | main.o  | (object)     | libs etc. |
          +---------+              +-----------+
               |                         |
Link...........|.........................|....
               |                         |
               +-------------------------+
               |
               V
          +---------+
          |  main   | (executable)
          +---------+

You fix that by either using turning the object file into an executable as you've done later in the process, though I would do it as:

cc -o size_of size_of.o

Or simply create the executable directly from the source file:

cc -o size_of size_of.c

And, if you're using make, make sure you have an actual Makefile. Otherwise, you get default rules which may not be what you want. It could be as simple as:

size_of: size_of.c Makefile
    gcc -o size_of size_of.c
Sign up to request clarification or add additional context in comments.

3 Comments

@harianja, you've already done that with the cc size_of.o -o size_of command in your question but I'll update the answer to make it clearer. But, for a simple one-source-file project, you don't have to worry, just use the gcc -o size_of size_of.c I've given.
Ok paxdiablo thanks for your help anyway can i have your email or facebook or anything to connect with you?
@harianja, glad I could help. As an aside, I wouldn't touch FB with a ten-foot pole and I'm old/paranoid enough that I guard my email as tightly as I guard my private parts in a gay bar :-) In any case, SO already gives you bucketloads of experts (myself among them, I'd like to think) for solving your problems - hence this is the correct place to connect.
0

In the first make invocation you are setting make target to be an object file (target has an .o extension). Built in make rule for object files is just to compile and assemble them (no linking) and that is what you get an object file.

Second invocation is actually asking make to build an executable file.

Gnumake has a set of built in rules for different targets. Please see this link for details:

https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.