Disable warning on fetched projects

Hi,

I wonder how I could disable warnings on header files, which are not part of my project (e.g. those in libaries I use and included via fetchContent).

For instance I get:
»bool std::uncaught_exception()« is depcreated [-Wdeprecated-declarations]

This is kinda annoying as I can’t really fix it, it’s not my code.

Don’t know if it helps or if it’s remotely right way but this is an example of what I do:

# Unfortunately, nlohmann_json does not set its headers as SYSTEM,
# so we get errors with our warning level.
get_target_property(_inc nlohmann_json::nlohmann_json INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(nlohmann_json SYSTEM INTERFACE ${_inc})

So in short - I find the right target and alter INTERFACE_INCLUDE_DIRECTORIES to be seen as SYSTEM. Solves the problem for me.

What do you mean by your comment with “Unfortunately the lib does not set its headers as SYSTEM” … is that some best practice for libs I didn’t know about?

Your solution works for me, although I wonder if there is a better way to do this. Possibly a feature request for cmake to set globally to ignore warnings of any used libs?

I especially don’t like about the approach that you have to know the internal name of the target.

I don’t know best practise but I’d say that the target you’re supposed to link against should have it set. That way the consumer doesn’t have to care. Many libraries do not do it which is a pity in my opinion. Somebody please correct me if I’m wrong.

That probably won’t work because after you fetch the external projects, you add it witch add_subdirectory() to your build tree. So it becomes a part of your build and all the targets become “your” targets - it’s no longer an external library. For that you’d probably need ExternalProject…

Me neither. I’m just saying it works and I have no time looking for more sophisticated solution.

While you could potentially solve this problem with cmake. I wouldn’t.

Here are some possible solutions.

  • Ask the maintainer of the codebase to fix their issues.
  • Create a header file wrapper, that includes the file and ignore the warnings using C++. Ex: “#pragma system_header”

I’ve posted some links on how to write the necessary C++.


Just to give a more exact solution

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 
#include "foo.h"
 
#pragma GCC diagnostic pop

Also in the above example if you wanna ignore all warnings possible in clang you can use -Weverthing instead!

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weverything" // clang only
 
#include "foo.h"
 
#pragma GCC diagnostic pop

If that only scaled… It might be a solution if you need 1-2 headers. I am not going to write something like that for each and every 3-party header (and keep it up to date with changes they introduce).

Asking the devs to fix it is fine. Except when you can only use released versions because of reasons and you need it now (like it usually is).

If you know the targets that are providing the headers in question, you could potentially add to that target’s INTERFACE_SYSTEM_INCLUDE_DIRECTORIES property to contain the directories where that target’s headers reside. Then anything linking to those targets should treat those headers as system headers, which should prevent compiler warnings being generated for them. Note that you can call set_property(TARGET blah APPEND PROPERTY INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ...) from any directory, not just the directory in which the target blah is defined.