Search code examples
c++makefileg++

Compiling .cpp files with 'g++'


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.


Solution

  • 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.