MFC dialog에서 콘솔(console)로 디버깅하기

Skills/Programming 2014. 12. 15. 15:09

MFC 다이얼로그를 이용하다 보면, 콘솔 응용프로그램을 코딩할 때처럼 printf 나 cout 같은 것으로 디버깅하고 싶을 때가 있다.. 그럴  떄 아래와 같은 방법으로 해결할 수 있다.


방법 1
App의 InitInstance에서 AllocConsole()을 호출한다.
디버그 모드에서만 동작할 것이므로 간단히 다음과 같이 할 수 있겠다.

#ifdef _DEBUG
    if( !AllocConsole() )
    {
        AfxMessage(_T("Failed to create the console!"), MB_ICONEXCLAMATION);
    }
#endif

해제하기 위해서는 ExitInstance에서 FreeColsole()을 호출한다.
이전과 마찬가지로 다음과 같이 쓰면 된다.

#ifdef _DEBUG
    if( !FreeConsole() )
    {
        AfxMessage(_T("Failed to free the console!"), MB_ICONEXCLAMATION);
    }
#endif

방법 2
stdafx.h에서 다음과 같이 입력한다.

#ifdef _DEBUG
#pragma comment(linker, "/entry:WinMainCRTStartup /subsystem:console")
#endif


출력은 일반적인 콘솔 프로그램과 같이 cout이나 printf 등을 사용하면 된다.

설정

트랙백

댓글