[macOS] CMake refuses to do a debug build

Hi there!

I’m trying to get cmake to pass -g2 to clang on macOS, and having tried everything in the man page and on Stack Overflow, I’m at a loss. Nothing I do seems to change its mind about whether its doing a Release or Debug build.

I’d really appreciate any pointers.

# How I'm setting up the build directory
adam@PrimeMover physics % cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug                                                                  
-- Failed to find LLVM FileCheck
-- git version: v0.0.0-dirty normalized to 0.0.0
-- Version: 1.5.4
-- Performing Test HAVE_THREAD_SAFETY_ATTRIBUTES -- failed to compile
-- Performing Test HAVE_STD_REGEX -- success
-- Performing Test HAVE_GNU_POSIX_REGEX -- failed to compile
-- Performing Test HAVE_POSIX_REGEX -- success
-- Performing Test HAVE_STEADY_CLOCK -- success
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/adam/SpacePhysics/physics/build

# How I'm running the build
cmake --build build --config Debug

# Another thing I've tried is putting this in CMakeLists.txt
set(CMAKE_BUILD_TYPE Debug)

None of the above seems to have any effect on the Makefiles cmake generates or what CXX_DEFINES and CXX_FLAGS are set during the build - all of them seem to be just whatever values clang has by default.

Any idea why this might be? I’m sure I’m missing something stupid.

By the way, I say I “just” want to pass -g2, but this is a bigger problem. cmake is also not, for example, declaring NDEBUG for release builds. It seems none of the configs have any effect on macOS build environments.

That sounds to me like the project might be overwriting variables like CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE. To be able to help any further, please reduce your project down to a minimal case that still reproduces your problem. More often than not, just trying to do that will uncover the problem.

I use CMake with many macOS users, and we use build types including Release, Debug, and RelWithDebInfo and haven’t had this kind of problem. We use clang, GCC, Intel compilers. Our project binary run times are often quite affected by the -O flags so I hear quickly if something isn’t working in that regard.

As Craig is saying, unfortunately some projects decide to get creative with forcing build settings. They might be including some file that force sets cache variable for CMAKE_BUILD_TYPE

You were right, a nested CMakeLists.txt was overriding the build type. Sorry for the noise.