-1

Hello im trying to add configuration specific definitions to my project but it doesn't seem to work the way i expect it to. My code works fine if i specify the configuration when i run cmake however this makes it a bit weird in visual studio since if i have something like the code below and specify -DCMAKE_BUILD_TYPE=Debug it will add the 'TestingDebug' define to all my configurations in visual studio which is not what i want.

if (CMAKE_BUILD_TYPE STREQUAL Debug)
add_definitions(-DTestingDebug)
endif()

if (CMAKE_BUILD_TYPE STREQUAL Release)
add_definitions(-DTestingRel)
endif()

Is there a way to generate all of these at onces so when i change configuration in visual studio it will have the correct defines?

i've tried using the cmake --build {builddir} --config {config} command because i read somewhere thats what you're supposed to use when trying to build for multi-configuration tools. The code below is what i used for my project but it doesn't seem to work either, idk if im just using it wrong or if there is something else i need to do.

cmake -S ../ -B ../Build/
cmake --build ../Build/ --config Debug
cmake --build ../Build/ --config Release
ExeQ
  • 23
  • 7
  • Variable `CMAKE_BUILD_TYPE` means nothing in Visual Studio (and other multiconfiguration generators). Instead use [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html). – Tsyvarev Feb 26 '23 at 18:16

1 Answers1

1

You can use add_compile_definitions which supports generator expressions. For example this one will add TestingDebug for Debug configuration and TestingRelease for Release.

add_compile_definitions(Testing$<CONFIG>)

Also you can use conditions inside generator expressions. This one will add TestingDebug for Debug configuration and TestingNotDebug for any other configuration:

add_compile_definitions($<IF:$<CONFIG:Debug>,TestingDebug,TestingNotDebug>)

More details: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html

jdfa
  • 659
  • 4
  • 11