GCC is a well known compiler for a number of languages. Here I will list and describe some important flags used with GCC, relevant for C and C++.
This is used to set the output file. Notice that it is small case ‘o’, not zero or upper case ‘O’. If the flag is absent, the output file is a.out
gcc -o myprog mycode.c
g++ -o myprog mycode.cpp
This is used to produce debugging information with output. This information can be used by debuggers like gdb, for debugging the program. I will advice against using this flag with optimization flag (below).
gcc -g mycode.c
g++ -g mycode.cpp
This is a highly recommended flag for all programs. It turns on all optional warnings. Such as “unused variable” warning. Needless to say you can ignore warnings, but a good programmer would want to know all warnings raised and fix them.
gcc -Wall mycode.c
g++ -Wall mycode.cpp
This is the most important flag. It controls optimization. While it can’t convert O(n2) solution to O(n)😉 it certainly can reduce running time drastically. See time comparison below. Notice that it is capital ‘O’, not zero. There are 3 levels (or rather 4) of optimization-
- -O1 : This tries to reduce execution time and code size. Thus it takes more time than usual. One example of optimization – transform conditional jump into branch-less equivalent.
- -O2 : This optimizes even more than -O1. Hence even better performance and more compilation time. For most purposes I use this flag for optimization.
- -O3 : Optimizes even more than -O2.
- -Os : This is little different, in that it optimizes for code size instead of execution time. But smaller code size also helps to reduce execution time.
gcc -O1 mycode.c
g++ -O1 mycode.cpp
I compiled the program to find prime numbers (using Sieve of Eratosthenes) with different optimization levels, here are various execution times and executable size-
Without optimization : 1.835 sec, 25.2 KB
-O1 : 0.489 sec, 13.7 KB
-O2 : 0.472 sec, 13.7 KB
-O3 : 0.460 sec, 17.5 KB
-Os : 0.573 sec, 13.9 KB
Now I warn you, don’t jump to any conclusion from these stats, they are just for showing that these flags are not useless😛
Used to set the language standard. Both C and C++ have several standards, like C++11, C++98, C99, C89. This flag is absolutely necessary if you want to use C++11 features, as its features are experimental currently in gcc and not available by default.
gcc -std=c99 mycode.c
g++ -std=c++11 mycode.cpp
Below flags are less important, but still useful to know.
Sometimes one needs to add #ifdef/#ifndef gaurds in code. For this I find -D flag extremely useful. It pre-defines a macro with definition 1. See example-
gcc -D DEBUG mycode.c
g++ -D DEBUG mycode.cpp
Above flag is equivalent to adding following line in C/C++ code
#define DEBUG 1
If you want to profile your code using gprof, you need to pass this flag.
gcc -pg mycode.c
g++ -pg mycode.cpp
These are not all flags, there are many many more. But I have covered most important ones. And I will add, if I find some other important flag. Enjoy!
Further reading : man page of gcc, optimize options