# ExternalProject\_Add and setting/appending to CMAKE\_CXX\_FLAGS

**URL:** <https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275>\
**Category:** Code\
**Created:** [May 26, 2020, 8:32pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275 "2020-05-26T20:32:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KrisThielemans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kristhielemans/32/532_2.png) [@KrisThielemans](https://discourse.cmake.org/u/KrisThielemans)\
**Post date:** [May 26, 2020, 8:32pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/1 "2020-05-26T20:32:59Z")

</div>

We use a SuperBuild and need to pass extra compiler flags on Windows with Visual Studio to the (CMake-based) external project. We have no control over the project.  
My strategy was to do something like

```auto
ExternalProject_Add(...
  CMAKE_ARGS -DCMAKE_CXX_FLAGS:STRING="-DBOOST_ALL_DYN_LINK"
)

```

I have 2 problems:

- this overrides `CMAKE_CXX_FLAGS` set by CMake itself. This is for Visual Studio Generator a disaster, as it sets `CMAKE_CXX_FLAGS=/DWIN32 /D_WINDOWS /W3 /GR /EHsc`. I suppose I could resolve this by using the existing `CMAKE_CXX_FLAGS` as well, but…
- I cannot find a way to set more than 1 flag this way, I’ve tried  
- `CMAKE_CXX_FLAGS="-DFlag1 -DFlag2"` (which actually sets a variable `Flag1 -DFlag2`)  
- `CMAKE_CXX_FLAGS=-DFlag1 -DFlag2` (which sets the first variable, but let CMake complain that `-DFlag2` is not a CMake flag)  
- `CMAKE_CXX_FLAGS=-DFlag1;-DFlag2` (which does the same as the previous variable)

You can see an attempt [here](https://github.com/SyneRBI/SIRF-SuperBuild/blob/3c5c673d045513dde766a655023f8f0c464400fd/SuperBuild/External_Boost.cmake#L102), from [a PR](https://github.com/SyneRBI/SIRF-SuperBuild/pull/401) to our SuperBuild.

I suppose we could use `PATCH_COMMAND` to patch the original project but according to the CMake doc that sounds difficult as we use `git`.

thanks for any help.

PS: Final piece of information is that we need to set `BOOST_ALL_DYN_LINK` on Windows to get the project to link Boost files, which is difficult as we don’t know what the user has installed for Boost libraries. (sadly `FindBoost` doesn’t take care of this, but that is off-topic).

---

<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:** [December 30, 2020, 7:52pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/2 "2020-12-30T19:52:28Z")

</div>

`FindBoost` should handle it with the `Boost::disable_autolinking` target (available since CMake 3.5.0).

---

<div class="post-metadata">

**Author:** ![KrisThielemans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kristhielemans/32/532_2.png) [@KrisThielemans](https://discourse.cmake.org/u/KrisThielemans)\
**Post date:** [January 23, 2021, 8:21am UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/3 "2021-01-23T08:21:24Z")

</div>

Thanks Ben. I didn’t know that target, so that’s useful information.  
I don’t see however how I can tell the independent project to use it (via `ExternalProject_Add`).

PS: In the mean time, I have figured out how to set 2 flags (the second bullet above) by doing for instance `-DCMAKE_CXX_FLAGS:STRING=-DBOOST_ERROR_CODE_HEADER_ONLY\ -DBOOST_SYSTEM_NO_DEPRECATED` (used [here](https://github.com/SyneRBI/SIRF-SuperBuild/blob/f5abe5b75fcadc8144082eb2ca84d1cb26f6a167/SuperBuild/External_Boost.cmake#L112-L121)). Still struggling with the first bullet.

---

<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:** [January 25, 2021, 3:31pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/4 "2021-01-25T15:31:06Z")

</div>

You should be able to add `-DBOOST_ALL_DYN_LINK` in the same way (or `-DBOOST_ALL_NO_LIB`) in the same way.

---

<div class="post-metadata">

**Author:** ![KrisThielemans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kristhielemans/32/532_2.png) [@KrisThielemans](https://discourse.cmake.org/u/KrisThielemans)\
**Post date:** [January 29, 2021, 1:59pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/5 "2021-01-29T13:59:02Z")

</div>

Thanks @ben.boeckel. However, my main problem is that this breaks the VS builds as I noted above.

---

<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:** [January 29, 2021, 2:16pm UTC](https://discourse.cmake.org/t/externalproject-add-and-setting-appending-to-cmake-cxx-flags/1275/6 "2021-01-29T14:16:32Z")

</div>

Hmm. I usually just figure that ExternalProject and multi-config generators are oil-and-water and don’t use them together (maybe it is better if every project is using CMake though; I’ve not had a superbuild with such luck so far though). Maybe someone else has better experience with them?
