Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does GCC behave if passed conflicting compiler flags?

Tags:

c

linux

gcc

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.

like image 716
Vilhelm Gray Avatar asked Apr 09 '13 18:04

Vilhelm Gray


People also ask

Which gcc flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

Does the order of compiler flags matter?

For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified.

What is the purpose of the flag when using gcc?

By default, GCC limits the size of functions that can be inlined. This flag allows the control of this limit for functions that are explicitly marked as inline (i.e., marked with the inline keyword or defined within the class definition in c++).

What is the usage of flag while using it with gcc for compiling c code?

This flag enables some optimizations and disables others. This option is enabled by default for the Java front end, as required by the Java language specification. Enable exception handling. Generates extra code needed to propagate exceptions.


1 Answers

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.

like image 124
Carl Norum Avatar answered Oct 24 '22 21:10

Carl Norum