What's the meaning of gcc '-c' and gcc '-o'?

Issue

When I created makefile, I wrote

test: main.o 1.o
  gcc -o test main.o 1.o

main.o: main.c a.h
  gcc -c main.c

1.o: 1.c a.h
  gcc -c 1.c

but I don’t get why I use -o in the first line and -c in the second, third line.

What’s the difference between them?

Solution

Those options do very different things:

  • -c tells GCC to compile a source file into a .o object file. Without that option, it’ll default to compiling and linking the code into a complete executable program, which only works if you give it all your .c files at the same time. To compile files individually so they can be linked later, you need -c.
  • -o sets the name of the output file that GCC produces. You’re using it when linking object files to make a complete program, and the default output filename for that is a.out. If you don’t want your program to be called a.out, you use -o to specify a different name.

Answered By – Wyzard

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published