개발툴

CMake에서 _DEBUG, NDEBUG 사용

j2b2 2023. 4. 26. 14:27

Visual Studio에서 디버그에서만 실행하고 싶은 코드는 아래와 같은 형태로 사용했다.

#ifdef _DEBUG
	// 디버깅에서만 동작하는 코드
#else
	// 디버그 모드가 아닐 때 동작하는 코드
#endif

 

VSCode에서는 같은 동작을 하게 하려면 CMakeLists.txt 상단에 아래와 같은 코드를 추가하면 Visual Studio에서 사용하던 방법과 같은 동작을 할 수 있다.

if (CMAKE_BUILD_TYPE STREQUAL "")
    # Build type is not set eg. command was "cmake .."
    message(STATUS "  Diag: Build type was unspecified, set to Release")
    set(CMAKE_BUILD_TYPE Release)
else ()
    message(STATUS "  Diag: Build type specified as '${CMAKE_BUILD_TYPE}'")
endif ()

if (${CMAKE_BUILD_TYPE} STREQUAL Debug)
    set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "_DEBUG")
else ()
    set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "NDEBUG")
endif ()

 

참고자료

https://stackoverflow.com/questions/58783805/define-debug-and-release-symbols-in-visual-studio-code