20

What does the -c flag do in gcc?

For example, what's the difference between

gcc -c output0.c

and

gcc output0.c

?

I know the second one makes a .a file, but I don't know what a .a file is.

Also, what does -o do in

gcc output0.o -o output0

Is it just to name the output file right?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Weadadada Awda
  • 353
  • 1
  • 4
  • 12
  • 3
    I recommend you read the [online documentation](http://gcc.gnu.org/onlinedocs/). For example, the section named ["3.2 Options Controlling the Kind of Output"](http://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Overall-Options.html#Overall-Options) – Some programmer dude Feb 06 '13 at 08:12
  • "Compile only" `gcc -c` produces only object file. To produce an executable, you have to call linker again. May be, `man gcc`? :) – P.P Feb 06 '13 at 08:12
  • Generally, looking at the other questions that you already asked, you need to learn how to use freely available information before bothering others with your questions. If you are on a linux system, `man` is your friend. And if you don't find it locally, use a search engine, first. – Jens Gustedt Feb 06 '13 at 08:20
  • I think the best you can do is read the manual page for gcc. – Pedro Rodrigues Feb 06 '13 at 08:12

3 Answers3

19

-c

Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. Unrecognized input files, not requiring compilation or assembly, are ignored.

-o file

Place output in file file. This applies regardless to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code. If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

More can be found in GCC Manual Page

Neuron
  • 5,141
  • 5
  • 38
  • 59
StarPinkER
  • 14,081
  • 7
  • 55
  • 81
5

From man gcc (section "Options Controlling the Kind of Output"):

...

-c Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o.

Unrecognized input files, not requiring compilation or assembly, are ignored.

...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GVerse
  • 126
  • 5
3

-c will instruct GCC to only compile the source file to an .o (object) file, but it does not invoke the linker.

With a project containing many .c files, one will typically compile first all .c files to .o files and then link everything together with the libraries.

-c

Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.

By default, the object file name for a source file is made by replacing the suffix >.c, .i, .s, etc., with .o.

Unrecognized input files, not requiring compilation or assembly, are ignored.

Source

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JoG
  • 6,612
  • 1
  • 15
  • 9