einpoklum
einpoklum

Reputation: 132086

Have a CMake project default to the Release build type

How should I make my CMake project default to configuration for a Release rather than a Debug build?

Upvotes: 7

Views: 8329

Answers (2)

einpoklum
einpoklum

Reputation: 132086

I started out with simplistic:

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release")
endif()

but thanks to @RaulLaasner's suggestions, I now do:

if (NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
endif()

and that seems to be more robust.


Edit: Upon later reflection, I've decided to not change the default from within the CMake script itself. Perhaps it's better to leave the default the same as with all other CMake scripts in the world basically.

Upvotes: 4

Raul Laasner
Raul Laasner

Reputation: 1585

You could try the following:

if (NOT EXISTS ${CMAKE_BINARY_DIR}/CMakeCache.txt)
  if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "" FORCE)
  endif()
endif()

Upvotes: 9

Related Questions