System Includes
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
- Cleaner build output: Eliminates warnings from third-party libraries
- Focus on your code: Makes it easier to spot real issues in your own code
- Standards compliance: Follows the intended use of system vs. user headers
- Tool compatibility: Works with both GCC and Clang
Best Practices
- Use
-isystemfor external libraries you don’t control - Use
-Ifor your own project headers where you want to see all warnings - In CMake, prefer
target_include_directories()withSYSTEMover globalinclude_directories()
This simple technique helps maintain clean build output while still catching important warnings in your own code.