phasor555
phasor555

Reputation: 265

I set CMAKE_BUILD_TYPE=Release, but I noticed that NDEBUG flag was not set. Does this mean it's a debug build?

I'm working on an embedded application with 3d graphics that requires high performance. While investigating performance issues, I noticed that NDEBUG flag is not set. I also have confirmed that CMAKE_BUILD_TYPE=Release. Isn't NDEBUG flag supposed to be set for release builds? Does NDEBUG flag not being set imply that this is a debug build?

I set it to Release build, but I also set it to -O2 optimization. Does this override the Release build and make it into debug?

Upvotes: 1

Views: 1780

Answers (1)

Hao.Dong
Hao.Dong

Reputation: 19

I have tested the following code with CLion on my PC. When CMAKE_BUILD_TYPE=Release, the program runs and exits with 0. When CMAKE_BUILD_TYPE=Debug, the assertion failed. It seems CMake or CLion automatically set NDEBUG when CMAKE_BUILD_TYPE is set to Release. You can also explicitly set NDEBUG to make sure functions like assert do not affect the performance of your embedded program.

#include <iostream>
#include <assert.h>

using namespace std;

int main(){
    assert(1 == 0);
    cout << "Hello world" << endl;
    return 0;
}

CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.10)
project(test_ndebug)

add_executable(test_ndebug main.cpp)

Upvotes: 1

Related Questions