Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This morning , I was trying to compile a C++ program using the following command on the command line

g++ -o foo.cpp foo

I spend about an hour trying t to resolve why I kept getting undefined reference & multiple definition error till I realised that my command to compile was wrong and it should be

g++ foo.cpp -o foo

I am just wondering what is the difference in process between g++ -o foo.cpp foo and g++ foo.cpp -o foo .

Why does placing the -o infront cause compilation error

share|improve this question

Because -o specifies the outfile. The first means that the outfile would be 'foo.cpp' whereas the second means that your outfile will be 'foo'.

The syntax of g++ is:

gcc [-c|-S|-E] [-std=standard] [-g] [-pg] [-Olevel] [-Wwarn...] [-pedantic] [-Idir...] [-Ldir...] [-Dmacro[=defn]...] [-Umacro] [-foption...] [-mmachine-option...] [-o outfile] [@file] infile...

Relevant man page: http://linux.die.net/man/1/g++

share|improve this answer

The switch -o takes an argument. In your case you were telling g++ to output the resulting compilation to the file foo.cpp. That's not what you want.

$ g++ -o foo.cpp foo

You want to output the resulting compilation of the source file (foo.cpp) to the binary file foo.

$ g++ foo.cpp -o foo

excerpt from g++ man page

-o <file>                Place the output into <file>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.