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

Marco Satti marcosatti at gmail.com
Fri Jun 2 23:03:13 EDT 2017


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.

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170603/f6562e2e/attachment.html>


More information about the CMake mailing list