ExternalProject_Add and setting/appending to CMAKE_CXX_FLAGS

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

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, from a PR 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).

1 Like

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

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). Still struggling with the first bullet.

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

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

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?