Henrik Sommerland Henrik Sommerland - 1 year ago 176
C Question

Why can't you use the -o flag with the -c flag in GCC?

Why is it so that one can not use the

flag in conjunction with the
flag in GCC?

I would like to store the generated object files in a different location from the source so not to clutter the src directory like so:

gcc src/foo.c src/bar.c -c -o bin/something.o

This does not work and without the -o flag the object file will be stored in the same location as the
is run from the and have the same name as the
file but with the

I get the following error message:

gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files

Is there any good reason for this?

Answer Source

"gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files"

-o specifies the name of the output file. But if you only compile without linking which -c requests, each source file creates its own output file. So which of those files should the compiler name as specified with -o? Solution: it is not legal and the compiler refuses to work.

If you want to rename each object file, add a line in your build-script or use e.g. mmv.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download