# Disable compiler flag for external dependencies

**URL:** <https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717>\
**Category:** Code\
**Tags:** os:linux, gen:makefiles\
**Created:** [December 19, 2021, 6:10pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717 "2021-12-19T18:10:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mint](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Mint](https://discourse.cmake.org/u/Mint)\
**Post date:** [December 19, 2021, 6:10pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/1 "2021-12-19T18:10:38Z")

</div>

Hello,

I am trying to compile my c++ source files with certain gcc compiler flags (namely `-Wall`), but not external libs. But I can’t figure out how to achieve this. I have this mock setup for my cmakelists project file:

```auto
cmake_minimum_required(VERSION 3.11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE "Release")

project(test)

add_subdirectory(extern/libs/efg)

include_directories("extern/libs/xyz")

add_executable(binary my_source_file1.cpp
			my_source_file2.cpp
			my_source_file3.cpp
			my_source_file4.cpp
)

target_compile_options(binary PRIVATE
				-Wall)

target_link_libraries(binary efg)

```

However when I build the project, it also gives compiler flag warnings for the external libs I have included. How would I avoid this? **I just want to get warnings for my own source files, and not any other external dependencies**. I am quite novice to cmake, so not sure how to achieve this

Thanks

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [December 20, 2021, 11:39am UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/2 "2021-12-20T11:39:53Z")

</div>

Your CMake file looks fine, are you sure you’re getting warnings for your external sources?

You most probably get warnings for their headers when included in your source files. And that’s how a C/C++ compiler works, the settings do apply to your source with anything included.

---

<div class="post-metadata">

**Author:** ![Mint](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Mint](https://discourse.cmake.org/u/Mint)\
**Post date:** [December 20, 2021, 1:18pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/3 "2021-12-20T13:18:05Z")

</div>

Hi,

Thanks for reply. Yeah, I am getting errors for libs added via the `add_subdirectory` command, as well as single header files (i.e. for both `efg` lib and the `xyz` header in the example above).  
Do you happen to know any way to ‘filter’ out header/extern libs from getting warnings? I have have compiled other projects (not mine), and they seem to have filtered out any external warnings. So I assume there is some cmake functions which can tell the compiler to prevent these warnings for given libs?

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [December 20, 2021, 3:15pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/4 "2021-12-20T15:15:39Z")

</div>

There is no such kind of filter in CMake AFAIK.  
If other projects are compiling fine, then it’s because they use external libraries without such problems. Or they disabled the compiler warnings (either command line or pragma).  
If the authors of the external library don’t care about warnings, the code quality might be low. It’s not that difficult to get the sources clean for the default warning level and headers even for `-Wall`.

---

<div class="post-metadata">

**Author:** ![Mint](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Mint](https://discourse.cmake.org/u/Mint)\
**Post date:** [December 20, 2021, 9:06pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/5 "2021-12-20T21:06:20Z")

</div>

Hmm alright.  
I have heard elsewhere that certain flags can be assigned to specific libs. E.g. if I wanted to assign `-w` to the `efg` lib (while keeping the rest of my cmake file untouched):

```auto

add_subdirectory(extern/libs/efg)
target_compile_options(binary PRIVATE
				-w)

```

But when I compile, it doesn’t seem to have an effect. Is it valid to set flags per lib like that? Not sure if I am doing something wrong with my setup tbh

---

<div class="post-metadata">

**Author:** ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)\
**Post date:** [December 21, 2021, 12:23pm UTC](https://discourse.cmake.org/t/disable-compiler-flag-for-external-dependencies/4717/6 "2021-12-21T12:23:10Z")

</div>

Yes, you’re doing that right. But this inhibits the warnings on the sources of the external library.  
So for any `.cpp` file of your external library you should not get any warnings.

Please have a detailed look at your make output. For which source file is the compiler called and which flags were used. Those with only `-w` won’t emit any warnings, and those with `-Wall` do, even for included headers of your external library. That’s how a C/C++ compiler works.
