4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
alryosha
  • 641
  • 1
  • 8
  • 15
  • 1
    Possible duplicate of [What does -c option do in GCC?](http://stackoverflow.com/q/14724315/608639) – jww Apr 02 '17 at 22:19

1 Answers1

17

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wyzard
  • 33,849
  • 3
  • 67
  • 87