LolloFake
LolloFake

Reputation: 65

How gcc multiple -o option works?

I'm trying to understand how the -o option of gcc works, I know that it's used to specify the output file name, but what happen when I write something like this:

gcc main.c -o test0 -o test1

or

gcc main.c -o test0 -o test1 -o test2

?
I have noticed that: only the last one -o <filename> is taken into account. So my question is: why I can specify multiple -o option? and how they can be useful ?

Upvotes: 1

Views: 688

Answers (2)

mafso
mafso

Reputation: 5543

This doesn't seem to be documented:

-o file Place output in file file. This applies to whatever sort of output is being produced, whether it be an executable file, an object file, an assembler file or preprocessed C code.

If -o is not specified, the default is to put an executable file in a.out, the object file for source.suffix in source.o, its assembler file in source.s, a precompiled header file in source.suffix.gch, and all preprocessed C source on standard output.

Playing around with it, it seems it is treated as a "global" option, allowed to specify only one output file, where only the last -o option given takes effect (as noted in the question).

$ gcc -c foo.c -o fooX.o bar.c -o barX.o
gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files
compilation terminated.

$ gcc -c foo.c -o fooX.o -o fooY.o # produces fooY.o only

Overriding previous options if a former one conflicts is common for many command-line tools, which sometimes can be useful when writing scripts. As a (perhaps not so good) example, consider

alias gcc='gcc -o my_default' # a.out is a bad default name

in your .bashrc; you can still type

gcc -o foo foo.c

without getting an error. (The example is bad, because gcc -c foo.c bar.c is invalid now, but hopefully it gives an idea of how something like this could be helpful.)

Upvotes: 3

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You can only have one output file, whose path is specified with the -o option.

Upvotes: 3

Related Questions