2013-07-19 1 views
0

저는 www.cplusplus.com을 사용하여 cpp로 OpenGL을 학습했습니다. 나는 코드 블록을 사용하고있는 프로그램이Windows에서 cpp가있는 윈도우 기능

/* Trim fat from windows*/ 
#define WIN32_LEAN_AND_MEAN 
#pragma comment(linker, "/subsystem:windows") 
/* Pre-processor directives*/ 
#include "stdafx.h" 
#include <windows.h> 
#include <iostream> 
/* Windows Procedure Event Handler*/ 
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
PAINTSTRUCT paintStruct; 
    /* Device Context*/ 
    HDC hDC; 
    /* Text for display*/ 
    char string[] = "Hello, World!"; 
    /* Switch message, condition that is met will execute*/ 
    switch(message) 
    { 
     /* Window is being created*/ 
     case WM_CREATE: 
      return 0; 
      break; 
     /* Window is closing*/ 
     case WM_CLOSE: 
      PostQuitMessage(0); 
      return 0; 
      break; 
     /* Window needs update*/ 
     case WM_PAINT: 
      hDC = BeginPaint(hwnd,&paintStruct); 
      /* Set txt color to blue*/ 
      SetTextColor(hDC, COLORREF(0x00FF0000)); 
      /* Display text in middle of window*/ 
      TextOut(hDC,150,150,string,sizeof(string)-1); 
      EndPaint(hwnd, &paintStruct); 
      return 0; 
      break; 
     default: 
      break; 
    } 
    return (DefWindowProc(hwnd,message,wParam,lParam)); 
} 
/* Main function*/ 
int APIENTRY WinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR  lpCmdLine, 
        int  nCmdShow) 
{ 
    WNDCLASSEX windowClass;  //window class 
    HWND  hwnd;    //window handle 
    MSG   msg;    //message 
    bool  done;    //flag saying when app is complete 
    /* Fill out the window class structure*/ 
    windowClass.cbSize = sizeof(WNDCLASSEX); 
    windowClass.style = CS_HREDRAW | CS_VREDRAW; 
    windowClass.lpfnWndProc = WndProc; 
    windowClass.cbClsExtra = 0; 
    windowClass.cbWndExtra = 0; 
    windowClass.hInstance = hInstance; 
    windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
    windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
    windowClass.lpszMenuName = NULL; 
    windowClass.lpszClassName = "MyClass"; 
    windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); 
    /* Register window class*/ 
    if (!RegisterClassEx(&windowClass)) 
    { 
     return 0; 
    } 
    /* Class registerd, so now create window*/ 
    hwnd = CreateWindowEx(NULL,  //extended style 
     "MyClass",   //class name 
     "A Real Win App",  //app name 
     WS_OVERLAPPEDWINDOW |  //window style 
     WS_VISIBLE | 
     WS_SYSMENU, 
     100,100,   //x/y coords 
     400,400,   //width,height 
     NULL,    //handle to parent 
     NULL,    //handle to menu 
     hInstance,   //application instance 
     NULL);    //no extra parameter's 
    /* Check if window creation failed*/ 
    if (!hwnd) 
     return 0; 
    done = false; //initialize loop condition variable 
    /* main message loop*/ 
    while(!done) 
    { 
     PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE); 
     if (msg.message == WM_QUIT) //check for a quit message 
     { 
      done = true; //if found, quit app 
     } 
     else 
     { 
      /* Translate and dispatch to event queue*/ 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 
    return msg.wParam; 
} 

처럼이 컴파일 마지막 SetTextColor, TextOut과 GetStockObject는 "정의되지 않은 참조"있는 링커 오류를 받고있다 간다. 나는 필요한 모든 파일을 포함하고 있지만, 마침내 다시 같은 문제에 빠지게됩니다.

저는 "stdafx.h"를 포함하고 있지만 성공하지 못했습니다. 나는 다시 같은 문제에 빠지게된다. 이 오류를 해결하는 방법은 무엇입니까?

+0

Wingdi.h, 이러한 함수가 선언 된 헤더를 포함 해 보았습니까? – melak47

+0

예. 그러나 효과가없는 것으로 보입니다. –

+0

'stdafx.h'에'windows.h'가 포함되어있는 한 괜찮습니다. –

답변

2

나는 어떤 이유로 여기에 코멘트를 할 수 없습니다, 그래서 대답에 넣어 것 :

뒤 당신이 정말이가 ("정의되지 않은") 컴파일러 오류가 있음을 의미? 링커 오류 ("정의되지 않은 참조") 인 경우 해당 라이브러리 (gdi32)를 포함하지 않았 음을 의미합니다.

+0

이것은 링커 오류입니다. 그렇다면 정확히 해당 라이브러리에 어떻게 잉크를 씁니까? –

+1

빌드 옵션 -> 링커 설정 -> 링크 Liraries로 이동하여 목록에 "gdi32.dll"을 추가하십시오. k. – Boise

+0

k. 고맙습니다. 그것은 성공했다 –

0

코드 블록은 미리 컴파일 된 헤더를 지원합니까? 그렇다면 stdafx.h 앞에 나오는 모든 것은 컴파일러에서 무시됩니다. cpp 파일에서 주석이 그 위에 있어야합니다.

stdafx.h의 목적은 컴파일간에 변경되지 않는 선언을 효율적으로 가져 오는 것입니다. 따라서 #define, #pragma 및 #include 문을 모두 stdafx.h 파일로 옮겨야합니다.

+0

나는 성공없이 그것을 시험해 보았다. –