[CMake] Provide default compiler/link flags in cache before project command.

Robert Maynard robert.maynard at kitware.com
Mon Jun 5 08:33:08 EDT 2017


You should look at the CMAKE_<LANG>_LANG_FLAGS_INIT (
https://cmake.org/cmake/help/v3.8/variable/CMAKE_LANG_FLAGS_INIT.html
) set of variables they are designed to resolve this issue.

On Fri, Jun 2, 2017 at 11:03 PM, Marco Satti <marcosatti at gmail.com> wrote:
> Hi, I am trying to let the user pick their own compiler/linker flags through
> the cache, whilst providing sane defaults.
>
>
>
> The problem is, I rely on the CMAKE_CXX_COMPILER_ID variable to set these
> defaults which is only available after the project() command is used. This
> also causes the CMAKE_CXX_FLAGS_{target} and CMAKE_EXE_LINKER_FLAGS_{target}
> to be populated in the cache by CMake, with defaults that do not work.
> Trying to set these flags after the project() command doesn’t work unless
> the FORCE flag is used… but then the user wouldn’t be able to override them.
>
>
>
> The workaround I used involved introducing a set of “USER_*” variables that
> are available in the cache (populated initially with the sane defaults),
> which then overrides the CMAKE_* variables above with the FORCE flag. It
> works, but I kind of think there should be a better way, especially since
> people who have used CMake before might try to set –DCMAKE_* using the
> command line or other wise and find it doesn’t work.
>
>
>
> Example from my CMakeLists.txt (working). I use INTERNAL instead of FORCE
> here so the user does not see this in the cmake-gui:
>
>
>
> project(myproject CXX)
>
>
>
> if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
>
>                set(USER_CXX_FLAGS_DEBUG           "-g -std=c++14"     CACHE
> STRING "CMake CXX compiler flags (Debug)."          )
>
>                set(USER_EXE_LINKER_FLAGS_DEBUG           "" CACHE STRING
> "CMake CXX linker flags (Debug)."            )
>
> elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
>
>                set(USER_CXX_FLAGS_DEBUG           "/MTd /Zi /Ob0 /Od /RTC1
> /MP /std=c++14" CACHE STRING "CMake CXX compiler flags (Debug)."          )
>
>                set(USER_EXE_LINKER_FLAGS_DEBUG           "/machine:x64
> /DEBUG" CACHE STRING "CMake CXX linker flags (Debug)."          )
>
> endif()
>
>
>
> … some lines below …
>
>
>
> set(CMAKE_CXX_FLAGS_DEBUG                  "${USER_CXX_FLAGS_DEBUG}"
> CACHE INTERNAL "")
>
> set(CMAKE_EXE_LINKER_FLAGS_DEBUG           "${USER_EXE_LINKER_FLAGS_DEBUG}"
> CACHE INTERNAL "")
>
>
>
> Ideally, I would like to be able to do something like this (doesn’t work):
>
>
>
> project(myproject CXX)
>
>
>
> if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
>
>                set(CMAKE_CXX_FLAGS_DEBUG           "-g -std=c++14"     CACHE
> STRING "CMake CXX compiler flags (Debug)."          )
>
>                set(CMAKE_EXE_LINKER_FLAGS_DEBUG           "" CACHE STRING
> "CMake CXX linker flags (Debug)."            )
>
> elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
>
>                set(CMAKE_CXX_FLAGS_DEBUG           "/MTd /Zi /Ob0 /Od /RTC1
> /MP /std=c++14" CACHE STRING "CMake CXX compiler flags (Debug)."          )
>
>                set(CMAKE_EXE_LINKER_FLAGS_DEBUG           "/machine:x64
> /DEBUG" CACHE STRING "CMake CXX linker flags (Debug)."          )
>
> endif()
>
>
>
> Thanks for your time!
>
> Marco.
>
>
>
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list