Home > Blockchain >  How to check for current CMake build type in C ?
How to check for current CMake build type in C ?

Time:05-21

A little background: I'm writing a game in SFML and I want certain lines of code to be ommitted in Release build. I want to check during compile time for build type, so that is doesn't impact game's performance.

void Tile::draw(sf::RenderTarget& target, sf::RenderStates states) const {
    states.transform *= getTransform();
    target.draw(m_sprite, states);
    #if(DEBUG)
        target.draw(m_collision_shape, states);
    #endif
}

I'm relatively new to CMake world, so I don't even know if it's possible.

EDIT: It's probably worth mentioning I use ninja as a build system.

CodePudding user response:

Add a macro from cmake in your code:

// CMakeLists.txt
add_executable(your_game yoru_soruces.c)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_definitions(your_game PRIVATE DEBUG=1)
else()
    target_compile_definitions(your_game PRIVATE DEBUG=0)
endif()

You could also instead use the standard macro NDEBUG, #if !NDEBUG, however it's better to leave it for asserts only. NDEBUG is not defined for CMAKE_BUILD_TYPE=Debug builds.

CodePudding user response:

That's how you might do it in general (working for any generator):

add_executable(${PROJECT_NAME} main.cpp)
target_compile_definitions(${PROJECT_NAME} PRIVATE "DEBUG=$<IF:$<CONFIG:Debug>,1,0>")
  • Related