# Compiler warning flags for all except ExternalProjects

**URL:** https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176
**Category:** Code
**Created:** [September 30, 2021, 12:41pm UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176 "2021-09-30T12:41:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)
#### Post date: [September 30, 2021, 12:41pm UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176/1 "2021-09-30T12:41:25Z")

</div>

I have an old cmake project and want to apply compiler warning flags for all targets except externals built with `ExternalProject_Add`. Is there any easy way to retro fit this?

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeling/32/1700_2.png) [@codeling](https://discourse.cmake.org/u/codeling)
#### Post date: [October 1, 2021, 11:23am UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176/2 "2021-10-01T11:23:30Z")

</div>

Can’t say for sure, but what I would try:

1. Check whether flags set via [`add_compile_options`](https://cmake.org/cmake/help/latest/command/add_compile_options.html#command:add_compile_options) are applied to your targets added via `ExternalProject_Add` as well; if not → GOOD!
2. Otherwise, do you have a list of all your “internal” targets? then you could loop over them and use [`target_compile_options`](https://cmake.org/cmake/help/latest/command/target_compile_options.html) to add the warning flags.
3. If it’s easier to get a list of all external projects, you could “subtract” that from the list of targets available via [`BUILDSYSTEM_TARGETS`](https://cmake.org/cmake/help/latest/prop_dir/BUILDSYSTEM_TARGETS.html) (you’d probably have to do this recursively for subdirectories, see also [this thread](https://discourse.cmake.org/t/cmake-list-of-all-project-targets/1077)), then use this list to do option 2.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 1, 2021, 11:37am UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176/3 "2021-10-01T11:37:09Z")

</div>

You can make a target that carries the flags and you link to from each of your targets (either manually or via something like looping over `BUILDSYSTEM_TARGETS`):

> [@Setting \`CMAKE\_CXX\_FLAGS\` within a function](https://discourse.cmake.org/t/setting-cmake-cxx-flags-within-a-function/4163/7):
>
> Note that the better solution these days is something like: add\_library(buildflags INTERFACE) target\_compile\_options(buildflags INTERFACE -Wsome-warning) # For each target target\_link\_libraries(mytgt PRIVATE buildflags)

---

<div class="post-metadata">

### Author: ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)
#### Post date: [October 1, 2021, 11:51am UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176/4 "2021-10-01T11:51:59Z")

</div>

I like the approach of having an interface target. My problem then becomes trying to figure out what syntax for `target_link_libraries` to use.

```cmake
target_link_libraries(mytgt PRIVATE buildflags) 

```

or

```cmake
target_link_libraries(mytgt buildflags) 

```

Apperently it is not possible to mix syntax for `target_link_libraries`.

> [@Consecutive target\_link\_libraries with and without PUBLIC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/7):
>
> Yes, it has such a default, for when PUBLIC/PRIVATE is omitted always. However, once you use such a keyword in one invocation, you have to use it in all of them. The way to think about it is to consider target\_link\_libraries() to have two distinct modes of operation: plain one, and keyword-based one. You can use it just fine in either mode, but you cannot mix modes for the same consuming target.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [October 1, 2021, 12:27pm UTC](https://discourse.cmake.org/t/compiler-warning-flags-for-all-except-externalprojects/4176/5 "2021-10-01T12:27:55Z")

</div>

Use `PRIVATE`.
