How to suppress compiler warnings from third-party libraries like Boost by using the -isystem flag instead of -I when including directories.

The Problem

When working with third-party libraries like Boost, you often encounter compiler warnings that originate from the library headers themselves, not your code. These warnings can clutter your build output and make it harder to spot genuine issues in your own code.

The Solution

Use the -isystem flag instead of -I to tell the compiler to treat the specified include directory as a system include directory.

Using g++

g++ -isystem /path/to/boost -o example example.cpp

Using CMake

In CMake, you can use the SYSTEM parameter:

target_include_directories(example SYSTEM PRIVATE /path/to/boost)

Or with the older include_directories() command:

include_directories(SYSTEM /path/to/boost)

How It Works

The -isystem flag tells the compiler to:

  • Treat headers in the specified directory as system headers
  • Suppress most warnings from these headers
  • Search these directories after the standard system directories

Benefits

  1. Cleaner build output: Eliminates warnings from third-party libraries
  2. Focus on your code: Makes it easier to spot real issues in your own code
  3. Standards compliance: Follows the intended use of system vs. user headers
  4. Tool compatibility: Works with both GCC and Clang

Best Practices

  • Use -isystem for external libraries you don’t control
  • Use -I for your own project headers where you want to see all warnings
  • In CMake, prefer target_include_directories() with SYSTEM over global include_directories()

This simple technique helps maintain clean build output while still catching important warnings in your own code.