Henrik Sommerland Henrik Sommerland - 1 month ago 9
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

-o
flag in conjunction with the
-c
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
gcc
is run from the and have the same name as the
.c
file but with the
.o
suffix?

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

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