# Disable warning on fetched projects

**URL:** https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978
**Category:** Code
**Created:** [October 7, 2020, 5:11pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978 "2020-10-07T17:11:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [October 7, 2020, 5:11pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/1 "2020-10-07T17:11:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 8, 2020, 6:05am UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/2 "2020-10-08T06:05:53Z")

</div>

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

```cmake
# 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.

---

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [October 8, 2020, 9:38pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/3 "2020-10-08T21:38:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 9, 2020, 6:35am UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/4 "2020-10-09T06:35:46Z")

</div>

> [@Leon0402](#):
>
> 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?

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.

> [@Leon0402](#):
>
> 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?

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…

> [@Leon0402](#):
>
> I especially don’t like about the approach that you have to know the internal name of the target.

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

---

<div class="post-metadata">

### Author: ![anon45792294](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@anon45792294](https://discourse.cmake.org/u/anon45792294)
#### Post date: [October 12, 2020, 5:00pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/5 "2020-10-12T17:00:45Z")

</div>

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++.

> **[warning pragma](https://docs.microsoft.com/en-us/cpp/preprocessor/warning?view=vs-2019)**

  

> **[How to Disable a Warning in C++](https://www.fluentcpp.com/2019/08/30/how-to-disable-a-warning-in-cpp/)**
>
> Expressive code in C++

---

<div class="post-metadata">

### Author: ![anon45792294](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@anon45792294](https://discourse.cmake.org/u/anon45792294)
#### Post date: [October 12, 2020, 5:03pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/6 "2020-10-12T17:03:37Z")

</div>

Just to give a more exact solution

```auto
#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!

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

```

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 12, 2020, 5:24pm UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/7 "2020-10-12T17:24:46Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [October 13, 2020, 3:30am UTC](https://discourse.cmake.org/t/disable-warning-on-fetched-projects/1978/8 "2020-10-13T03:30:38Z")

</div>

If you know the targets that are providing the headers in question, you could potentially add to that target’s [`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES`](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_SYSTEM_INCLUDE_DIRECTORIES.html) 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.
