2011-07-27 48 views
2

我遇到以下CPP代码的问题:http://ideone.com/XZXZJDirectX和C++

我收到以下错误:

(1) Warning  1  warning LNK4042: object specified more than once; extras ignored  c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\Debug\WinMain.obj 1 

(2) Error  2  error LNK1561: entry point must be defined  c:\Users\jabbott\documents\visual studio 2010\Projects\DirectX9_Tutorial\DirectX9_Tutorial\LINK 

找不到入口点设置未设置。我试图把它设置为WinMain,但没有运气。

// WinMain.h 
#ifndef APP_HPP_ 
#define APP_HPP_ 
#include <Windows.h> // Windows API Library 
#include <d3d9.h>    // Include DirectX 9 Library 
#include <d3d10_1.h> // Include DirectX 10 Library 
#include <d3d11.h>    // Include DirectX 11 Library 


/* Global Functions */ 
// Handle any messages from MS Windows 
LRESULT CALLBACK WndProc(HWND hWnd, 
         UINT uMsg, 
         WPARAM wParam, 
         LPARAM lParam); 

// Main Entry Point 
int WINAPI WinMain(HINSTANCE hInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR lpCmdLine, 
       int nShowCmd); 

/* Global Variables */ 
IDirect3D9    *g_pD3D;    // DirectX 3D v9 Instance 
IDirect3DDevice9  *g_pD3DDevice; // DX3D9: Representation of Graphics Card 
LPCTSTR ClsName = "g_szInterfaceClass"; 
//IDirect3D10 *g_pD3D10;  // DirectX 3D v10 Instance 
//IDirect3DDevice10  *g_pD3DDevice10;  // DX3D10: Representation of Graphics Card 
//IDirect3D11 *g_pD3D11;  // DirectX 3D v11 Instance 
//IDirect3DDevice11  *g_pD3DDevice11;  // DX3D11: Representation of Graphics Card 

#define WINDOW_WIDTH 200 
#define WINDOW_HEIGHT 200 

/* Error Macro for Debugging */ 
//#define ERROR(msg) { MessageBox(NULL, msg, L"Error", MB_OK|MB_ICONEXCLAMATION); } 
#endif // WINMAIN_H 

WinMain.cpp:

// WinMain.cpp 
#include "WinMain.h" 


LRESULT CALLBACK WndProc(HWND hWnd, 
         UINT uMsg, 
         WPARAM wParam, 
         LPARAM lParam) 
{ 
     switch (uMsg) 
     { 
       case WM_CLOSE: 
         PostQuitMessage(0); 
       break; 
     } 

     return DefWindowProc(hWnd, 
               uMsg, 
               wParam, 
               lParam); 
} 


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */ 

int WINAPI WinMain(HINSTANCE hInstance, 
       HINSTANCE hPrevInstance, 
       LPSTR lpCmdLine, 
       int nShowCmd) 
{ 
     // The WNDCLASSEX structure contains window class information 
     WNDCLASSEX  wc; 
       wc.cbSize  = sizeof(WNDCLASSEX); 
       wc.style   = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
       wc.lpfnWndProc = (WNDPROC) WndProc; 
       wc.cbClsExtra = 0; 
       wc.cbWndExtra = 0; 
       wc.hInstance  = hInstance; 
       wc.hIcon   = LoadIcon(NULL, IDI_APPLICATION); 
       wc.hCursor  = LoadCursor(NULL, IDC_ARROW); 
       wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
       wc.lpszMenuName = NULL; 
       wc.lpszClassName = ClsName; 
       wc.hIconSm  = LoadIcon(NULL, IDI_APPLICATION); 

     // rect structure to define the window dimensions 
     RECT rect; 
     rect.top    =  (long) 0; 
     rect.left    =  (long) 0; 
     rect.right    =  (long) WINDOW_WIDTH; 
     rect.bottom    =  (long) WINDOW_HEIGHT; 

     // Register the window to MS Windows 
     if(!RegisterClassEx(&wc)) 
     { 
       MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 
       return 0; 
     } 

     // calculate the required size of the window rectangle, based on the desired size of the client rectangle 
     AdjustWindowRectEx( &rect, 
               WS_OVERLAPPEDWINDOW, 
               FALSE, 
               WS_EX_APPWINDOW | WS_EX_WINDOWEDGE); 

     // create the target window of the application 
     HWND hWindow = CreateWindowEx(NULL, 
                 ClsName, 
                 "Name of Window", 
                 WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
                 0, 0, 
                 rect.right-rect.left, 
                 rect.bottom-rect.top, 
                 NULL, NULL, 
                 hInstance, 
                 NULL); 

     if (!hWindow) 
     { 
       DestroyWindow(hWindow); 
       UnregisterClass(ClsName, hInstance); 
       MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); 
       return 0; 
    } 

    ShowWindow(hWindow, SW_SHOW); 
    UpdateWindow(hWindow); 
    SetForegroundWindow(hWindow); 
    SetFocus(hWindow); 

    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.Windowed     = true; 
    d3dpp.hDeviceWindow  = hWindow; 
    d3dpp.BackBufferCount = 1; 
    d3dpp.BackBufferWidth = WINDOW_WIDTH; 
    d3dpp.BackBufferHeight = WINDOW_HEIGHT; 
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
    d3dpp.SwapEffect    = D3DSWAPEFFECT_DISCARD; 


    // Identify current version of Direct3D 
    g_pD3D =Direct3DCreate9(D3D_SDK_VERSION); 

    // Create the Direct3D Interface Device. Thisisanabstracted version of 
    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, 
                D3DDEVTYPE_HAL, 
                hWindow, 
                D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
                &d3dpp, 
                &g_pD3DDevice))) 
    { 
      MessageBox(NULL, "Failed to create Direct3D InterfaceDevice.", "Error!", MB_ICONEXCLAMATION | MB_OK); 
    } 

    /* Main Game Boot */ 

    // Enter the message loop 
    MSG msg; 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
      // Clear the target render buffer black 
      g_pD3DDevice->Clear( 0, 
              0, 
              D3DCLEAR_TARGET, 
              D3DCOLOR_XRGB(0, 0, 0), 
              1.0f, 
              (DWORD) 0.0f); 

      // Send the buffer to the computer monitor 
      g_pD3DDevice->Present(NULL, NULL, NULL, NULL); 

      //Send any messages to WndProc function 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
    } 


    g_pD3D->Release(); 

    return 1; 
} 

回答

3

你的第一个问题可以通过删除这一行来解决:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Predeclaration of WinProc() */ 

您已经位于其上方的功能的实现。如果函数的实现发生在函数被调用的位置或实现位于不同的翻译单元(源文件)之后,您只需要上述内容。一般来说,尽量避免功能的“预先声明”,如果可以的话,使用函数本身作为声明,这将减少所需的维护量。

第二个问题可能是因为您正在编译控制台应用程序(入口点main)而不是Windows应用程序(入口点WinMain)。

+0

这是正确答案(+1)。另外,我想补充一点,你已经在头文件中声明了函数原型,所以即使你没有在WinMain中调用它之前定义函数,它仍然会正确编译。 –