1

I am an Ubuntu user. What is the difference between g++ file.cpp -o file and g++ -c file.cpp?

I know that g++ -c file.cpp creates an object file, file.o.

But what about g++ file.cpp -o file?

I have been using this command for a long time, but I don't know the file generated as an output (it is just "file"). I have to run ./file to execute the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pratyparty
  • 15
  • 2
  • 8
  • 1
    While it could be a little overwhelming, [the GCC manual](https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc/) is the best place to find the answers to questions about its command-line options. – Some programmer dude Jul 25 '21 at 13:08
  • 3
    The `-c` and `-o` options are covered in ["Options Controlling the Kind of Output"](https://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/Overall-Options.html#Overall%20Options). – G.M. Jul 25 '21 at 13:17
  • What is the meaning of "Compile or assemble the source files, but do not link. The linking stage simply is not done". Thanks for the help @G.M. – Pratyparty Jul 25 '21 at 13:22
  • @Pratyparty Re. compiling and linking, you'll get some useful info from [this post](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work). – G.M. Jul 25 '21 at 13:32
  • The "natural" way to build a source file is to generate an executable program file. There are options to either stop at intermediate steps or to generate different kinds of output. For example you could ask GCC to stop after preprocessing with the `-E` option; Or to stop and generate only assembly output with the `-S` option; Or to stop after creating an object file with the `-c` option. – Some programmer dude Jul 25 '21 at 13:34
  • To look at the preprocessed output or the generated assembly could be useful for debugging, while stopping after creating object files are common when working on larger projects where you have many source files which are compiled into object files and then linked together into the final executable file. Also, static libraries are created as archives of object files. – Some programmer dude Jul 25 '21 at 13:35
  • Linking is essentially taking all object files and all libraries to create the final executable program file. – Some programmer dude Jul 25 '21 at 13:36
  • So, everything comes down to g++ (GNU) and fundamentals of OS (linkers, preprocessor and compilers). I have now gotten the idea of how it works behind the scenes. Thanks for the help @G.M. – Pratyparty Jul 25 '21 at 13:42
  • @Someprogrammerdude I have gone through the documentation of GCC. It was really helpful. Appreciate your help – Pratyparty Jul 25 '21 at 13:44
  • It is nearly impossible to navigate the GCC documentation with a search engine (especially the short/unspecific ones like "-o") and [the man page](https://linux.die.net/man/1/gcc) only includes a small subset of the options. But a trick is to discover the existence of *** *** *** *** *** ***[the options index](https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html)*** *** *** *** *** *** !!!! (No, it has to wrangled from the search engines or be discovered by accident.) Note that the entries do ***not*** include the leading "`-`". – Peter Mortensen May 04 '23 at 22:06
  • cont' - For example, there are [eight entries starting with "-c"](https://gcc.gnu.org/onlinedocs/gcc/Option-Index.html#Option-Index_op_letter-C). – Peter Mortensen May 04 '23 at 22:06
  • I have also mapped the [most commonly used ones](https://pmortensen.eu/EditOverflow/_Wordlist/EditOverflowList_latest.html#--coverage). E.g., "-o" can be [looked up directly](https://pmortensen.eu/world/EditOverflow.php?LookUpTerm=-o). – Peter Mortensen Jun 20 '23 at 21:18

3 Answers3

4

With -o you can specify the output file name. In your, e.g., g++ file.cpp -o file means: compile file.cpp to file.

Without -o your source code will compile to a.out file.

If you worry about others option in g++, you can always use g++ --help. It will show you all parameters and their meanings. For example, -o meaning from help:

-o <file> : Place the output into <file>

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

g++ file.cpp -o file produces an executable file (which normally have no extensions on Linux). -o specifies the output file name. If you do just g++ file.cpp, the file will be named a.out.

It's equivalent to g++ -c file.cpp followed by g++ file.o -o file, except that the file.o is not saved anywhere.

Note that you can use -o <filename> with -c as well, to change the name of the object file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
0

-o simply means "output" and allows you to choose your output file's name. It remains totally allowed to specify both -c and -o if you need it.

Simply, cc used to name a final, linked executable a.out for historical reasons and will still keep on doing it for while, when using -c is primarily meant to compile multiple parts before assembling from the beginning, thus preserving the original name.

That's why see more often -o specified when compiling final executable than other types of compiled files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Obsidian
  • 3,719
  • 8
  • 17
  • 30