c++cmakeboost

CMake `SYSTEM` Flag Not Suppressing Warnings from Boost Header-Only Libraries


I'm trying to compile a project with CMake. The project has a few header-only libraries, and I include them like so:

target_include_directories(cartogram
  PRIVATE
    ${PROJECT_SOURCE_DIR}/include
  SYSTEM
    ${Boost_INCLUDE_DIRS}
    PkgConfig::fftw
    PkgConfig::cairo
    $<$<BOOL:${APPLE}>:/opt/homebrew/include>
)

However, despite using the SYSTEM flag, I still get warnings from boost like so:

In file included from /Users/adi/Desktop/github/cartogram-cpp/src/inset_state/write_inset_to_geojson.cpp:1:
In file included from /Users/adi/Desktop/github/cartogram-cpp/include/inset_state.hpp:6:
In file included from /Users/adi/Desktop/github/cartogram-cpp/include/geo_div.hpp:4:
In file included from /Users/adi/Desktop/github/cartogram-cpp/include/ellipse.hpp:4:
In file included from /Users/adi/Desktop/github/cartogram-cpp/include/cgal_typedef.hpp:11:
In file included from /opt/homebrew/include/CGAL/Polyline_simplification_2/simplify.h:29:
In file included from /opt/homebrew/include/CGAL/Modifiable_priority_queue.h:20:
In file included from /opt/homebrew/include/boost/heap/pairing_heap.hpp:21:
/opt/homebrew/include/boost/heap/detail/stable_heap.hpp:100:32: warning: unused parameter 'rhs' [-Wunused-parameter]
  100 |     size_holder(size_holder && rhs) BOOST_NOEXCEPT

Ideally, I'd like to remove all errors from external libraries, but just using the SYSTEM flag does not seem to do that.

When I additionally include the boost libraries (again, so twice total) target_include_directories like so:

  target_compile_options(cartogram PRIVATE -isystem ${Boost_INCLUDE_DIRS})

The warnings go away.

Here is what I get after running cmake -B build:

❯ cmake -B build && sudo make install -j -C build
-- The CXX compiler identification is AppleClang 16.0.0.16000026
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/homebrew/lib/cmake/Boost-1.86.0/BoostConfig.cmake (found version "1.86.0") found components: unit_test_framework
-- Found PkgConfig: /opt/homebrew/bin/pkg-config (found version "0.29.2")
-- Checking for one of the modules 'fftw3'
-- Checking for one of the modules 'cairo'
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/adi/Desktop/github/cartogram-cpp/build

Solution

  • Unlike to PRIVATE/PUBLIC/INTERFACE keywords, which may be issued at any time in target_include_directories command and affect only the following arguments of the command, the SYSTEM keyword should come as the first argument, and it affects on all directories specified in the command.

    Specification of non-system include directories and system include directories should use separate target_include_directories calls:

    # Non-system includes
    target_include_directories(cartogram
      PRIVATE
        ${PROJECT_SOURCE_DIR}/include
    )
    # System includes
    target_include_directories(cartogram
      SYSTEM
      PRIVATE
        ${Boost_INCLUDE_DIRS}
        $<$<BOOL:${APPLE}>:/opt/homebrew/include>
    )
    

    When SYSTEM keyword is issued as non-first argument of target_include_directories command, it is silently treated as a directory name.