Search code examples
c++debuggingcmakeclion

Cant debug `iostream` related code when using MSVC toolchain in clion


I am new to c++ development, and I find that I can't place breakpoints in iostream related class and if I force step into call of iostream functions like basic_streambuf::xsgetn I will get a disassembly view which I have absolutely no interest to read.

I am using clion with Visual Studio Toolchain. Is there a way to debug normally with those classes?

By the way, how to use lldb to get actual value of unique_ptr, now I am using ptr._Mypair._Myval2 which seems really ugly....


Solution

  • After a few days of digging. It turns out that adding the following to CMakeLists.txt fixed the problem

    if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
        message(STATUS " MSVC DEBUG RUNTIME")
        # enable debugging of runtime library
        set_property(TARGET archive_patcher_cpp PROPERTY
                MSVC_RUNTIME_LIBRARY "MultiThreadedDebug")
    endif()
    

    Note: this will link runtime statically so the executable size will be a lot bigger, and this requires cmake 3.15 or higher.

    reference: https://cmake.org/cmake/help/v3.15/prop_tgt/MSVC_RUNTIME_LIBRARY.html

    For cmake lower than 3.15:

    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")