# Propagating CXX flags to out of source projects?

**URL:** <https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138>\
**Category:** Code\
**Created:** [November 6, 2020, 9:59pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138 "2020-11-06T21:59:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 6, 2020, 9:59pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/1 "2020-11-06T21:59:33Z")

</div>

I need to propagate some cross-compiler flags like -mcpu down to some lower level libraries that are out of source. Is there “CMake way” to do this or do I need to agree upon some global variable name convention and “pass” the flags that way? For example…

# In source

proj/source/CMakeLists.txt  
set(global\_cxx\_flags -mcpu=cortex-m4)  
add\_directory(…/…/somelib somelib EXCLUDE\_FROM\_ALL)

# Out of source …/…/somelib/CMakeLists.txt

target\_compile\_options(somelib  
PRIVATE ${global\_cxx\_flags}  
)

Seems icky.

---

<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:** [November 6, 2020, 11:13pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/2 "2020-11-06T23:13:30Z")

</div>

add\_compile\_options

[https://cmake.org/cmake/help/latest/command/add\_compile\_options.html?highlight=add\_compile\_options](https://cmake.org/cmake/help/latest/command/add_compile_options.html?highlight=add_compile_options)

This is what I use to propagate compiler flags like /Zi and /MP in our drivers.

Supports generator expressions, uses properties, etc.

---

<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:** [November 7, 2020, 12:29am UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/3 "2020-11-07T00:29:21Z")

</div>

Is this what you wanted?

---

<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:** [November 7, 2020, 1:10am UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/4 "2020-11-07T01:10:58Z")

</div>

Btw please only use add\_compile\_definitions if you are the top level project. Otherwise projects that absorb you may have to go through unnecessary hurdles.

---

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 7, 2020, 1:12am UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/5 "2020-11-07T01:12:47Z")

</div>

Oh I totally get that. I just don’t know how to deal out of source projects. I can’t rearrange the dir structure.

---

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 7, 2020, 8:15pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/6 "2020-11-07T20:15:27Z")

</div>

No. I’ve read that page many times and it works great for subdirs of my project. It does not work for out of source builds. That is my question, how to push flags to out of source builds which I admit is odd but I cannot change the dir structure for this build.

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [November 9, 2020, 1:22am UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/7 "2020-11-09T01:22:33Z")

</div>

Has something failed with CXX flags when using `add_subdirectory()`? I don’t recall having any issues with `add_subdirectory(src_dir bin_dir)` and `src_dir` wasn’t in the same directory as the top-level source directory; and this was with using toolchains and cross-compiling. In general you wouldn’t expect to have to do anything special. How do you define the CXX flags? As part of CMAKE\_CXX\_FLAGS\_INIT?

---

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 9, 2020, 12:41pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/8 "2020-11-09T12:41:38Z")

</div>

`add_subdirectory()` works fine. `add_compile_definitions()`, however, does not propagate to out-of-source dirs added via `add_subdirectory()` as far as I can tell. I try not to touch any `CMAKE_xxx` variables.

Again, assume this dir structure where some\_other\_proj and some\_proj are peers.  
some\_other\_proj  
\_ source  
some\_proj  
\_ source (CMakeLists.txt)

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [November 9, 2020, 2:03pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/9 "2020-11-09T14:03:40Z")

</div>

Like I said this works for me in CMake 3.16. I’ve tested both `add_compile_definitions()` and `add_compile_options()`. Both of these propagate correctly when using `add_subdirectory(../src2 src2)`. The command I use when running CMake is `cmake -S src1 -B test_build`. Where src1 and src2 are at the same directory level.

```auto
$ cat src1/CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(ProjectName)

#add_compile_definitions(testflag1)
add_compile_options(-Dtestflag1)
add_subdirectory(../src2 src2)
add_executable(exe_test file1.c)
target_link_libraries(exe_test lib2)

```

---

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 9, 2020, 2:14pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/10 "2020-11-09T14:14:33Z")

</div>

Okay, thank you for forcing me to re-read what I wrote in my CMakeLists.txt. I, of course, was wrong. I was using `target_compile_definitions()`!

---

<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:** [November 10, 2020, 11:45am UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/11 "2020-11-10T11:45:35Z")

</div>

Good to know I wasn’t going insane, Mark would you mind marking my answer as correct?

---

<div class="post-metadata">

**Author:** ![mrfirmware](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mrfirmware/32/934_2.png) [@mrfirmware](https://discourse.cmake.org/u/mrfirmware)\
**Post date:** [November 10, 2020, 12:07pm UTC](https://discourse.cmake.org/t/propagating-cxx-flags-to-out-of-source-projects/2138/12 "2020-11-10T12:07:12Z")

</div>

Oops sorry. Done. Thanks again for your help.
