cmake ignores -D CMAKE_BUILD_TYPE=Debug
Asked Answered
I

3

17

I'm just trying to build a cmake project in debug-mode to enable asserts. I tried the following versions:

cmake -D CMAKE_BUILD_TYPE:STRING=Debug -L ../../
cmake -DCMAKE_BUILD_TYPE:STRING=Debug -L ../../
cmake -DCMAKE_BUILD_TYPE=Debug -L ../../

Unfortunately none of theese has the desired effect - that CMAKE_BUILD_TYPE is set to Debug (and therefore the NDEBUG-flag is not passed to gcc).

Additionally I added variable_watch(CMAKE_BUILD_TYPE) to my main CMakeLists.txt to check whether the value get's overridden somewhere. But the first output is a READ_ACCESS in my main Additionally I added variable_watch(CMAKE_BUILD_TYPE) to my main CMakeLists.txt and the value there already is Release.

Does someone have an idea why cmake ignores the configuration?

I'm using cmake version 2.8.7.

Il answered 28/5, 2014 at 9:25 Comment(6)
Have you checked the contents of CMakeCache.txt?Schreck
Yes, there is set CMAKE_BUILD_TYPE=Release - and even changing this manually and then rerunning cmake has not effect (the value in CMakeCache.txt get's overwritten again).Il
Is it related to this? comments.gmane.org/gmane.comp.kde.devel.general/46310Talkative
No, not really. My problem is not that the CMAKE_BUILD_TYPE=Debug does not have the effect it should have but instead that I cannot set CMAKE_BUILD_TYPE to Debug..Il
I already cleaned the build directory and tried again which also did not have any effect. And: You're right. I'm using Unix-Makefiles.Il
Related to stackoverflow.com/questions/46820155.Ohaus
I
19

Ok, fgrep -R "CMAKE_BUILD_TYPE" finally found the problem for me. In some CMakeLists.txt-file I found something like that:

SET( CMAKE_BUILD_TYPE Release ... FORCE )

That overrides every user defined parameters (because of the FORCE).

What works for me is that:

IF( NOT CMAKE_BUILD_TYPE )
   SET( CMAKE_BUILD_TYPE Release ... FORCE )
ENDIF()

Thank's for your hints!

Il answered 28/5, 2014 at 15:58 Comment(3)
Does it work for you? For me, using Ninja as the target of CMake, it doesn't seem to work. What version of CMake do you use?Ohaus
On modern CMake (Say 3.10 and up) see KitWare Blog - CMake and the Default Build Type.Ohaus
What is the ... for? An ellipsis replaces text that was left out. Or is it a literal ...?Katricekatrina
O
2

I assume that there is something wrong with your config..

I wrote a complete, simple example here: https://dl.dropboxusercontent.com/u/68798379/cmake-build-type.tar.bz2

cmake_minimum_required (VERSION 2.8)

project(playlib)

message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")

IF(CMAKE_BUILD_TYPE MATCHES Debug)
  message("Debug build.")
ELSEIF(CMAKE_BUILD_TYPE MATCHES Release)
  message("Release build.")
ELSE()
  message("Some other build type.")
ENDIF()

add_library(TESTLIB SHARED src/test.c)

When you execute cmake with

cmake -DCMAKE_BUILD_TYPE=Debug ../../

It gives the following output:

$ ./gen-linux.sh
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMAKE_BUILD_TYPE = Debug
Debug build.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wojci/stack-overflow/cmake-build-type/build/linux

It shows that CMAKE_BUILD_TYPE is being set from the command line and it being recognized in the CMakeLists.txt config.

What happens when you run it on your system using your version of CMake?

Opinionated answered 28/5, 2014 at 15:22 Comment(1)
what if someone make this cmake -DCMAKE_BUILD_TYPE=DEBUG?Year
G
0

This works for me:

IF(${CMAKE_BUILD_TYPE} MATCHES Debug)
...
ENDIF()
Glamour answered 15/4, 2020 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.