Reputation: 78914
If I give clang -O2 -O3
in the same command line, in that order, is the -O3
going to override the -O2
? Does the later argument always override?
A build script which I can't change by default adds -O2
and I can only add things after it. Is that an acceptable thing to do?
Upvotes: 9
Views: 992
Reputation: 941455
Operation of the Clang driver is described in the manual page Driver Design & Internals § Driver stages. Note how you can use the -###
option to get it to dump the result of each stage. This is not something you can exercise with your borken build system since the option must be listed first. But you can verify that the driver does in fact do what you hope it does:
clang -### foo.cpp -O2 -O3 # dumps yayayada "-O3" yadamore
clang -### foo.cpp -O3 -O2 # dumps yayayada "-O2" yadamore
Where “yada” is spew that I omitted since there’s too much of it. So, indeed, the last -O
option you specify is the one that is effective. Which is the expected behavior for any compiler driver.
Upvotes: 9
Reputation: 72639
clang
processes options left-to-right. Thus, the last -O
option "wins". This is a feature exactly for the reason you ask: so there's a possibility to override defaults set by someone else (e.g. some build system, software developers, ...) Yes, it is totally acceptable, and you are in plenty of good company.
The ultimate reference would be the LLVM source code (option handling
is implemented by cl::ParseCommandLineOptions()
in file lib/Support/CommandLine.cpp
.)
Thinking outside the box: even if you cannot change the build script, you may influence it to do what you want. For example, the optimization option may be part of a variable that is taken from an option or from the environment. For example, if the build uses a Makefile
, the variable could be called CFLAGS
or COPTS
and be set with
make CFLAGS=-O3
If the build uses a shell script, maybe something like
CFLAGS="-O3" ./configure
would work. There's no telling without seeing the build.
Upvotes: 5