CMake: set warning flags for my project, but not included headers

Viewed 245

I am building a project that includes llvm. I want to enable warnings for my code, but ignore warnings from code in llvm headers.

I have a source file like:

// main.cpp
#include <llvm/IR/Instructions.h>

void foo() {
   // blah blah blah
} 

and a CMake file like:

add_executable(prog main.cpp)
target_link_libraries(prog ${llvm_libs})
target_compile_options(prog PRIVATE "-Weverything")

But the -Weverything produces a huge number of warnings in the included llvm files, when I only care about seeing warnings in my own code.

How can I enable warnings for my code, without also seeing warnings for the included llvm headers?

1 Answers

You need to correctly configure include dirs in your CMake file. Something like this should do the trick:

target_include_directories(prog <your project-specific include dirs>)
target_include_directories(prog SYSTEM ${LLVM_INCLUDE_DIRS})

This way compiler will treat LLVM headers as "third-party" and won't emit any warnings for them.

Related