At invocation time, normally all that you need to make a debug build with CMake is:
cmake -DCMAKE_BUILD_TYPE=Debug ..
Doing that adds the -g flag as can be confirmed with:
make VERBOSE=1
which shows the GCC build commands as explained at: https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands
-O0 is not added by default to the Debug build, but -O0 is the default -O value as mentioned at: https://stackoverflow.com/questions/1778538/how-many-gcc-optimization-levels-are-there and on man gcc, so it normally doesn't matter.
If you would really like to explicitly control the build flags for the debug you can also set them with
cmake -DCMAKE_BUILD_TYPE=Debug
-DCMAKE_C_FLAGS_DEBUG="-g -O0" \
-DCMAKE_CXX_FLAGS_DEBUG="-g -O0" \
..
The default values of CMAKE_C_FLAGS_DEBUG and CMAKE_CXX_FLAGS_DEBUG can be found as mentioned at: https://stackoverflow.com/questions/16851084/how-to-list-all-cmake-build-options-and-their-default-values with:
cmake -LAH .
which gives:
// Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
// Flags used by the C compiler during DEBUG builds.
CMAKE_C_FLAGS_DEBUG:STRING=-g
That command also shows other values of interest which clarify the default behavior across built types:
// Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=
// Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=-g
// Flags used by the CXX compiler during MINSIZEREL builds.
CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
// Flags used by the CXX compiler during RELEASE builds.
CMAKE_CXX_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
// Flags used by the CXX compiler during RELWITHDEBINFO builds.
CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
If -DCMAKE_BUILD_TYPE=Debug is not passed, CMAKE_BUILD_TYPE is empty, and none of the extra CMAKE_CXX_FLAGS_XXX values are added, so we end with a build without -g.
Tested on Ubuntu 22.10, CMake 3.24.2.