For example, when I entered either:

gcc -O hello.c -c


gcc hello.c -c -O

Both did not complain.

Does the order of command options matter?


This depends on the program itself; the operating system doesn't dictate whether or not order matters.

GCC's set of options is so colossal that I can't say with any authority if you can supply any option in arbitrary order; you'll have to read the documentation for that option. That said, a general rule of thumb is that if you have two or more mutually exclusive options (such as -O1 -O2 for differing levels of optimisation), programs will generally take later options over earlier ones. Again, this isn't enforced by linux.

A simple program that does let you specify most options in any order would be ls. Listing all files in the current directory with detail can be done with either ls -la, ls -al or ls -l -a. However, ls -l1 (that is 'el' 'one') does not give the same output as ls -1l ('one' 'l'). These are mutually exclusive options, and the last listed over rides the first given.

There is also the odd program which applies options to arguments as they arrive. So, for example, you might have a hypothetical command blah -a 1 2 -b 3 where -a applies to all three arguments, but -b only applies to 3.

Again, this is up to the individual program in question. If you're ever unsure, read the documentation.


There are cases where the order of command line options matters even in GCC. If you are linking with static libraries (.a), then if you specify -llib1 -llib2 and there's a function in liblib2.a that calls a function in liblib1.a that was not brought into the program, then the link will fail with an unresolved symbol. With shared libraries, this is not a problem.

In general, as others said, the order of the options may or may not make a difference. However, the output from the two commands below are different - so the order of the arguments to cat alters the output:

cat /etc/passwd /etc/group
cat /etc/group  /etc/passwd

Note also that on Linux (in particular), GNU getopt() is apt to reorder the command line so that all the options (starting with a minus) are processed before any of the other arguments - unless you use double-dash -- to mark the end of the arguments, or unless you set the environment variable POSIXLY_CORRECT.


Only if you have 2 options that are mutually exclusive. Otherwise, order does not matter.

Of course, this can vary depending on how the program has been written, but should apply to all normal *nix tools.


Hard to know, as other already told you it may make a difference (or not).

A good rule of thumb is to open the man page and look at the first example and use that order when putting the arg in there.

So if we look at the cat command (man cat):

       cat [OPTION] [FILE]...

It seems like as long as all the options are before the file args you should be fine.

And if we look at the gcc beast (man gcc):

       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-pedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] [@file] infile...

       Only the most useful options are listed here; see below for the remainder.  g++ accepts mostly
       the same options as gcc.

It is not that simple to understand as the cat command :)

But if you would like to play it safe, -c seems to come before -O and then infile (hello.c) seems to be last.

gcc -c -O hello.c

But as you already know, since the others work... this is playing it very safe :)

  • What about linking flags like -static-libstdc++? – Royi Sep 2 '18 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.