0

How is one supposed to properly use CMake for setting build configurations in a cross-platform way?

Currently i do this:

set(BUILD32 false)

if(BUILD32)
    set(CMAKE_C_FLAGS -m32)
    set(CMAKE_CXX_FLAGS -m32)
endif()

But from what i've googled that's bad and i should be using 'generator expressions'. I've found this: CMAKE_BUILD_TYPE not being used in CMakeLists.txt

that says to use:

target_compile_definitions(MyTarget PUBLIC "$<$<CONFIG:Debug>:MYDEBUG_MACRO>")

But i'm not sure how the CONFIG:Debug is supposed to be set, or what the MYDEBUG_MACRO is supposed to be.

All i want is an option the ability to compile in release, debug, and x86/x64 variations of those two for both makefiles and Visual studio.

I appreciate any help.

Stephen Eckels
  • 435
  • 6
  • 17
  • `But i'm not sure how the CONFIG:Debug is supposed to be set` - It is `CMAKE_BUILD_TYPE` which is selected by a user (via command line or in IDE; see [that question](https://stackoverflow.com/questions/19024259/how-to-change-the-build-type-to-release-mode-in-cmake) for Visual Studio). `what the MYDEBUG_MACRO is supposed to be.` - Any macro which you want to be defined when build type is `Debug`. Debug-specific compiler flags which are not a macro definitions, should be set via `CMAKE_C_FLAGS_DEBUG`(https://cmake.org/cmake/help/v3.7/variable/CMAKE_LANG_FLAGS_DEBUG.html) variable. – Tsyvarev Aug 05 '17 at 19:26
  • As for x86/x64 variations, it is unlikely you may use build type for distinguish them: while for `gcc` compiler flag `-m32` is the only difference for them, Visual Studio uses different **generators** (do not confuse with *generator expressions*). See e.g. description of [Visual Studio 14 2015](https://cmake.org/cmake/help/v3.7/generator/Visual%20Studio%2014%202015.html) generator. – Tsyvarev Aug 05 '17 at 19:30
  • So once cmake_build_type is set does cmake automatically set the generator to use debug mode, or is my macro supposed to manually set the compiler flags like I did before. I'm not sure which things are handled for me or which I have to set myself as flags. Perhaps a minimalistic example? I would accept that as an answer, and thank you! – Stephen Eckels Aug 06 '17 at 00:37
  • [This answer](https://stackoverflow.com/a/11437693/3440745) explains how to define custom build type. Try to use it for your purposes. – Tsyvarev Aug 06 '17 at 13:45

0 Answers0