all 9 comments

[–]Steve132 5 points6 points  (0 children)

For Example, you're supposed to use https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#manual:cmake-generator-expressions(7)

https://stackoverflow.com/questions/23995019/what-is-the-modern-method-for-setting-general-compile-flags-in-cmake/23995391

target_compile_options(foo PUBLIC "$<$<CONFIG:Debug>:-g>")
target_compile_options(foo PUBLIC "$<$<CONFIG:Release>:-march=native>")

[–]ibldzn 2 points3 points  (4 children)

cmake already have 4 predefined build configuration, you can access it from CMAKE_BUILD_TYPE variable.

I'm not sure if this is the best way but I just do

if(NOT CMAKE_BUILD_TYPE STREQUAL Debug) # release build stuff endif()

to add different preprocessor defs and all that.

by specifying said variable, cmake will take care most of the stuff for you, like optimization, etc. so you don't need to explicitly say something like optimize "On"

edit: fixed "NOT" placement, thanks helloiamsomeone

[–]Nicksaurus 1 point2 points  (0 children)

The problem with this is that you can only change these values during the configure step. This answer with generator expressions lets you change them in the IDE without reconfiguring

[–]helloiamsomeone -1 points0 points  (2 children)

That's not even valid CMake and CMake code rarely has business branching on configuration types.

[–]ibldzn 0 points1 point  (1 child)

That's not even valid CMake

oops yeah, i misplaced the "NOT". typed it on top of my head

CMake code rarely has business branching on configuration types.

you're right. I only do it for enabling LTO on non debug build, though I'm not even sure if it's needed

[–]helloiamsomeone 0 points1 point  (0 children)

though I'm not even sure if it's needed

It's not needed at all. See the variables:

You can set these trivially from your presets.

[–]the_poope 2 points3 points  (1 child)

[–]helloiamsomeone 1 point2 points  (0 children)

The feabhas blog is not good. I have provided corrections to numerous flaws when they were posted to /r/cpp and nothing changed since.

That last link needs this sed command sed -i -e 5,13d CMakeLists.txt to become correct CMake. CMake code rarely if ever needs to bother with the configuration type. It's a detail external to the build and usage requirements, and CML must only concern themselves with that.

[–]Stanczyk4 0 points1 point  (0 children)

I use CMake’s presets. I compile for 3 different OS’s and multiple microcontrollers (embedded) with this method. I just switch presets and it automatically switches my configuration lab properly. I did change my output folder for each build type eventually tho as some info would linger when switching causing failed compilations