[CMake] Per target, per configuration compile flags issue

John van der Burg john at mysticgd.com
Sat Apr 18 20:01:56 EDT 2015


I'm having some problem with custom configurations in combination with
multiple cmake projects.
Basically what I have is some helper function/macro which sets the
available configurations, sets the CMAKE_CXX_FLAGS_<configname> etc.

I do this with:

set(CMAKE_CONFIGURATION_TYPES Debug Release)
set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
"The available configuration types." FORCE)

Then per target, i set the compiler and linker flags per configuration.
Such as:

set(CMAKE_CXX_FLAGS_DEBUG "/MP /GS /analyze- /Zc:wchar_t /Zi /Gm- /Od /Ob1
/fp:fast /errorReport:prompt /Zc:forScope /GR- /Gd /Oy- /Oi /EHa /nologo /D
\"DEBUG\" /D \"_DEBUG\" ${CUSTOM_CXX_WARNINGFLAGS} ${CUSTOM_CXX_ARCH_FLAGS}
${CUSTOM_CXX_RUNTIMEFLAG} ${CUSTOM_CXX_OPENMP}" CACHE STRING "Flags used by
the C++ compiler during this build." FORCE )
set(CMAKE_C_FLAGS_DEBUG  "" CACHE STRING "Flags used by the C compiler
during this build." FORCE )
set(CMAKE_EXE_LINKER_FLAGS_DEBUG  "${CUSTOM_LINKER_FLAGS} /DEBUG" CACHE
STRING "Flags used for linking binaries during this build." FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG  "${CUSTOM_LINKER_FLAGS} /DEBUG" CACHE
STRING "Flags used by the shared libraries linker during this build." FORCE
)
set(CMAKE_STATIC_LINKER_FLAGS_DEBUG  "${CMAKE_STATIC_LINKER_FLAGS_DEBUG}
${CUSTOM_LINKER_FLAGS} /DEBUG" CACHE STRING "Flags used by the static
libraries linker during this build." FORCE )
MARK_AS_ADVANCED(CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_SHARED_LINKER_FLAGS_DEBUG
CMAKE_STATIC_LINKER_FLAGS_DEBUG)
#-------------------------------------------------------------------------------------------------------------
set(CMAKE_CXX_FLAGS_RELEASE "/MP /GS /analyze- /Zc:wchar_t /Zi /Gm- /O2
/Ob2 /fp:fast /errorReport:prompt /Zc:forScope /GR- /Gd /Oy- /Oi /EHa
/nologo /Ot /D \"NDEBUG\" ${CUSTOM_CXX_WARNINGFLAGS}
${CUSTOM_CXX_ARCH_FLAGS} ${CUSTOM_CXX_RUNTIMEFLAG} ${CUSTOM_CXX_OPENMP}"
CACHE STRING "Flags used by the C++ compiler during this build." FORCE )
set(CMAKE_C_FLAGS_RELEASE  "" CACHE STRING "Flags used by the C compiler
during this build." FORCE )
set(CMAKE_EXE_LINKER_FLAGS_RELEASE  "${CUSTOM_LINKER_FLAGS}" CACHE STRING
"Flags used for linking binaries during this build." FORCE )
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE  "${CUSTOM_LINKER_FLAGS}" CACHE
STRING "Flags used by the shared libraries linker during this build." FORCE
)
set(CMAKE_STATIC_LINKER_FLAGS_RELEASE  "${CUSTOM_LINKER_FLAGS}" CACHE
STRING "Flags used by the static libraries linker during this build." FORCE
)
MARK_AS_ADVANCED(CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE
CMAKE_EXE_LINKER_FLAGS_RELEASE CMAKE_SHARED_LINKER_FLAGS_RELEASE
CMAKE_STATIC_LINKER_FLAGS_RELEASE)


Now I'm making some project that uses add_subdirectory(...) to add other
cmake projects.

But doing that seems to overwrite the settings all the time with flags set
by other projects.
So if i adjust the warning level in one project then once generated (for
visual studio), it has the warning level of another cmake project, as that
has overwritten the compiler flags.

Am I doing something wrong here?
I tried to not cache and force the values, but that didnt work at all. Then
CMake uses its default values everywhere.

Basically I need unique per project, per configuration compiler and linker
flags, but somehow I can't manage to get that working as when i
add_subdirectory other projects they overwrite each others settings.

If someone has some hints, that would be cool :)

Also I noticed there is a COMPILE_FLAGS target property for
set_target_properties
That can be used to setup additional flags per target. But it has no
support for different configurations it seems, while the LINKER_FLAGS
option (i think its named like that) has, which seems odd.


Cheers,
- John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150419/88d7135a/attachment.html>


More information about the CMake mailing list