0

I have a CMakeLists.txt like this:

project(MyProject)
add_subdirectory(SomeLibrary)
add_executable(MyProject ${SRC_FILES})
target_compile_options(MyProject PRIVATE -Werror -Wall)
target_link_libraries(MyProject SomeLibrary)

Where SomeLibrary is a 3rd party library (linked as a git submodule) that will fail to build with -Werror -Wall

I want to build my project with -Werror -Wall, but disable -Werror for the subproject. How can I do this?

I saw a related question, but that covers the case where the flags are set directly through CMAKE_CXX_FLAGS - not target_compile_options.

  • 2
    Do the errors occur in the library's cpp files or in the header files you include from your project? BTW, consider raising an issue with the third party library. Compiling with `-Wall` isn't too much to ask for. – Friedrich May 31 '23 at 13:50
  • 1
    Does [this](https://stackoverflow.com/questions/64064157/is-there-a-way-to-get-isystem-for-fetchcontent-targets) help? `-isystem` is a flag to ignore warnings from headers in specific locations (and more). – Mestkon May 31 '23 at 13:54
  • 1
    @Friedrich Oh, nice catch! I feel kind of silly for not realising, but the issue was actually in one of the imported headers. I guess there's no way around that then, except maybe disabling the specific warnings through compiler-specific "#pragma" directives (`#pragma GCC diagnostic ignored` etc.). And yes, I'm planning to make a PR fixing these. Though, I know of one closed-source library we're using where that isn't really an alternative (a huge library containing *hundreds* of problematic headers). Not sure what's the best way to deal with that tbh. Thanks for pointing this out! – MyNiceDisplayName May 31 '23 at 15:59
  • @MyNiceDisplayName sometimes you're too close to see the issue. Happens to all of us. You should write a short answer, though. You will be able to accept your answer after 48 hours. Thanks. – Friedrich May 31 '23 at 17:31
  • @Friedrich Thanks! It's not the first time haha :) And yes, good idea! I'll do that. – MyNiceDisplayName May 31 '23 at 17:44
  • Does this answer your question? [Is there a way to get -isystem for FetchContent targets?](https://stackoverflow.com/questions/64064157/is-there-a-way-to-get-isystem-for-fetchcontent-targets) – starball May 31 '23 at 19:26

1 Answers1

2

Answering my own question (thanks to @Friedrich for pointing me in the right direction):

Turns out target_compile_options does after all work per-target, and I had simply misread the compilation output. The errors came from a header exposed by the 3rdparty library, which was included from my own project. So the third party library builds fine, since it is built without -Werror, but my main project (which is built with _Werror) fails.

I guess the only way to fix this is to either:

  • Fix the problematic headers yourself (and notify maintainer / make PR).
  • Use compiler-specific #pragma directives (#pragma GCC diagnostic ignored or #pragma warning on MSVC) to disable the specific warnings when including the problematic headers.