G++ Cheatsheet

GCC, the GNU Compiler Collection, is used to build your C++ executables. If you’re interested in reading about GCC, you can check out the GNU website. You can also take ITP 439, which covers compiler development. If you’re looking for how to compile your programs before submitting, check the compiling homework section below.


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.

Bonus Round

Here are a couple compiler flags that we don’t as often use in this class. They’re still pretty handy to know. You can read about all the options for using g++ here.

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 (that’s an O as in Oreo, or Optimize) to the rescue! 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 (you’ll learn about these more in CSCI 356). 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.

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++17 main.cpp -o main

# After, we get more helpful warnings about potential bugs
$ g++ -O2 -Wall -std=c++17 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 [-Werror=maybe-uninitialized]
  return static_cast<AVLNode<Key,Value>*>(this->mRight);
avl/avlbst.h:199:23: note: 'y' was declared here
  AVLNode<Key, Value>* y;

You can read more about -O2 online.

Cheat Sheet

# Compile a single program
g++ -Wall -g -std=c++17 test.cpp -o test

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

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

# Compile and optimize in parallel for helpful warnings about potential bugs
g++ -O2 -Wall -std=c++17 main.cpp -o main

We compile your homework with a lot of extra flags. Make sure you compile with the following flags and fix the errors before submitting your homework. If you want to use a Makefile instead of writing out these flags every time, check out our guide on Makefiles.

# use the following warnings to compile your program
-std=c++17 -pedantic -Wall -Wextra -Werror -Wshadow -Wsign-conversion -g

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