24

I know that if you execute GCC as such:

gcc -O3 -O2 foo.c 

GCC will use the last optimization flag passed (in this case O2). However, is this true for all flags? For example, if I execute GCC like so:

gcc -mno-sse -msse bar.c 

Will it support SSE since that was the last flag passed, or would this result in undefined behavior? My initial experimentation seems to indicate that it will support SSE, but I'm not sure if this is true for all cases.

0

1 Answer 1

25

Normally later options on the line override ones passed previously, as you mention in your first example. I haven't personally come across any different behaviour for -m or -f flags, but I don't know of a specific reference in the documentation.

Note that some options don't behave this way:

$ gcc example.c -DABC -DABC=12 <command-line>: warning: "ABC" redefined <command-line>: warning: this is the location of the previous definition 

So there would need to be a -UABC in between there to shut that warning up.

As an aside, clang is particularly good at solving this problem - it will produce a warning if it ignores a command line option, which can help you out.

Sign up to request clarification or add additional context in comments.

3 Comments

It's possibly worth noting that gcc still behaves in the "later options on the line override ones passed previously" way, even in your example. It's just that it also gives you a warning.
main(){return 0;} will compile silently with no flags, and will produce warning: return type defaults to ‘int’ when called with -std=c99 -Wno-implicit-int -Wno-implicit, in every order possible.
The docs for warning options do say ("The combined effect of positive and negative forms is that more specific options have priority over less specific ones, independently of their position in the command-line. For options of the same specificity, the last one takes effect."), but not the optimization or other options docs. However, you can inspect the final options list that is used to compile by using -frecord-gcc-switches, which will show that the last one is used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.