g++ cheatsheet

gcc/g++ Cheatsheet


GCC (the GNU Compiler Collection) is used to build your C++ executables. It’s a powerful tool and you kinda have to use it to test your programs. For a quick tldr to compile your program with maximum warnings, look here


Here are a few of the more useful command options when using g++. These compiler flags may be used in any order you wish. However, by convention, we usually group the list of source files together and the list of options together.

Most Common

These are generally most important options for normal use on your VM and in your assignment Makefiles.

You’ll probably end up knowing them by heart.

  • -Wall show all warnings. It turns on all standard C++ warnings about code that might cause unexpected behavior.
  • -g provides debugging feature for your program. You will need this when you want to use gdb or valgrind.
  • --std=c++<##> uses version <##> of C++ when compiling. This will allow you to use specific features of that C++ version.
  • -o <filename> compiles and links files into an executable named <filename>. The default filename is a.out.
  • -c compiles and assembles files but doesn’t link them. This is useful when building large projects to separate file compilation and minimize what is re-compiled.

Bonus Round

Here are a couple compiler flags that we don’t as often use. They’re still pretty handy to know.

  • --sys-root=<directory> uses <directory> as root directory for headers and libraries.
  • -I /<absolute-path> adds <absolute-path> to the compiler’s search paths. As written, the path must written from the root of the filesystem, /.
  • -Werror make all warnings into errors do this before submitting your code to avoid getting points deducted!
  • -pedantic issues all warnings demanded by strict ISO C++ rules if you want to be extra safe
  • -Wextra enables some extra warnings not turned on by -Wall. These include warnings for bad pointer to integer zero comparisons, base class not initialized in copy constructor of derived class, etc.
  • -Wfatal-errors is similar to -Wall but treats an error as fatal and stops before dumping a long list of errors into the terminal.
  • -fmax-errors=n tells the compiler to stop after encountering n errors in your code. Usually you want to see all errors in your code some error messages may get too long for you to read.
  • -Wshadow warns whenever a local variable or type declaration shadows another variable, parameter, class member, etc.
  • -Wsign-conversion or -Wconversion warn if making unsafe, implicit conversions between signed and unsigned types (i.e. unsigned int + (constant expression) )
  • -O2 optimize code and check for errors in parallel to find potential bugs. (read more about this here)
  • --help gets help on specific options for the compiler.

More Error Checks

Sometimes you’ll have a program that seems to work but will produce a ton of Valgrind errors. You’ll get some fairly strange errors about invalid reads or confusing c++ library functions. Ever thought the compiler might be able to help solve these issues before you had to slog through pages of these debugger errors? Introducing the compiler flag -O2 to the rescue! (that’s an O as in Oreo, or Optimize) Normally, when you compile c++ programs, you’ll ask for all warnings with the -Wall flag. After the compiler checks for these errors, it will build your program with a ton of fancy optimizations to make your code faster*. The problem with this is sometimes you’ll miss a few potential errors by not including the optimization step. You can use the -O2 flag to optimize and error check at the same time.

* you can learn about these more in a computer architecture class such as USC’s fantastic CS356!

For example, a student’s code seem to be perfectly normal when compiling with the normal flags.

# before, compiled without errors
g++  -Wall -std=c++11 main.cpp -o main

# after, we get more helpful warnings about potential bugs
g++ -O2 -Wall -std=c++11 main.cpp -o main

In file included from main.cpp:9:
avl/avlbst.h: In member function
'void AVLTree<Key, Value>::balance(AVLNode<Key, Value>*&)
[with Key = std::__cxx11::basic_string<char>; Value = int]':
avl/avlbst.h:106:54: error: 'y' may be used uninitialized in this function
  return static_cast<AVLNode<Key,Value>*>(this->mRight);
avl/avlbst.h:199:23: note: 'y' was declared here
  AVLNode<Key, Value>* y;

Reference about the -O2 flag


# compile a single program
g++ -Wall -g -std=c++11 test.cpp -o test

# print all possible warning flags available to compile with
g++ --help=warnings

# compile program using header files in another directory
#   include header files in ./libs directory
#   $(pwd) will expand to your current working directory
g++ -I /$(pwd)/libs -Wall -g -std=c++11 test.cpp -o test

Compiling Programs

This was written for CS104, the most difficult required C++ course at USC. Homework assignments are compiled with a lot of extra flags to ensure programs run as expected. To follow the CS104 submission standards, compile your programs with the following flags.

# use the following warnings to compile your program
-pedantic -Wall -Wextra -Werror -Wshadow -Wsign-conversion

# example compilation
g++ -g -std=c++11 -pedantic -Wall -Wextra -Werror -Wshadow -Wconversion -Wunreachable-code homework_program.cpp -o homework_program

Additional Resources

You can read about all the options for using g++ here.