2012-03-27 110 views
1

我在拼命地嘗試使用DirectX進入C++編程。這是一場陡峭的艱苦戰鬥,我只寫了幾行代碼。我的大部分戰鬥都是在鏈接器上進行的。到目前爲止,我有這個在我的頭文件:DirectX SDK的C++鏈接器錯誤

#pragma once 

#include "resource.h" 
#include <d3d11.h> 
#include <d3dx11.h> 
#include <d3dx10.h> 

#pragma comment (lib, "d3d11.lib") 
#pragma comment (lib, "d3dx11.lib") 
#pragma comment (lib, "d3dx10.lib") 

IDXGISwapChain *swapChain; 
ID3D11Device *device; 
ID3D11DeviceContext *deviceContext; 

void InitializeDirect3D(HWND hWnd); 
void CleanDirect3D(void); 

和源文件中:

// InitializingDX.cpp : Defines the entry point for the application. 
// 

#include "stdafx.h" 
#include "InitializingDX.h" 

#define MAX_LOADSTRING 100 

// Global Variables: 
HINSTANCE hInst;        // current instance 
TCHAR szTitle[MAX_LOADSTRING];     // The title bar text 
TCHAR szWindowClass[MAX_LOADSTRING];   // the main window class name 

// Forward declarations of functions included in this code module: 
ATOM    MyRegisterClass(HINSTANCE hInstance); 
BOOL    InitInstance(HINSTANCE, int); 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 

int APIENTRY _tWinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPTSTR lpCmdLine, 
        int  nCmdShow) 
{ 
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine); 

    // TODO: Place code here. 
    MSG msg; 
    HACCEL hAccelTable; 

    // Initialize global strings 
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_INITIALIZINGDX, szWindowClass, MAX_LOADSTRING); 
    MyRegisterClass(hInstance); 

    // Perform application initialization: 
    if (!InitInstance (hInstance, nCmdShow)) 
    { 
     return FALSE; 
    } 

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_INITIALIZINGDX)); 

    // Main message loop: 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
     { 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
    } 

    return (int) msg.wParam; 
} 



// 
// FUNCTION: MyRegisterClass() 
// 
// PURPOSE: Registers the window class. 
// 
// COMMENTS: 
// 
// This function and its usage are only necessary if you want this code 
// to be compatible with Win32 systems prior to the 'RegisterClassEx' 
// function that was added to Windows 95. It is important to call this function 
// so that the application will get 'well formed' small icons associated 
// with it. 
// 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    WNDCLASSEX wcex; 

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style   = CS_HREDRAW | CS_VREDRAW; 
    wcex.lpfnWndProc = WndProc; 
    wcex.cbClsExtra  = 0; 
    wcex.cbWndExtra  = 0; 
    wcex.hInstance  = hInstance; 
    wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_INITIALIZINGDX)); 
    wcex.hCursor  = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 
    wcex.lpszMenuName = MAKEINTRESOURCE(IDC_INITIALIZINGDX); 
    wcex.lpszClassName = szWindowClass; 
    wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 

    return RegisterClassEx(&wcex); 
} 

// 
// FUNCTION: InitInstance(HINSTANCE, int) 
// 
// PURPOSE: Saves instance handle and creates main window 
// 
// COMMENTS: 
// 
//  In this function, we save the instance handle in a global variable and 
//  create and display the main program window. 
// 
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 

    hInst = hInstance; // Store instance handle in our global variable 

    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 

    if (!hWnd) 
    { 
     return FALSE; 
    } 

    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    return TRUE; 
} 

// 
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM) 
// 
// PURPOSE: Processes messages for the main window. 
// 
// WM_COMMAND - process the application menu 
// WM_PAINT - Paint the main window 
// WM_DESTROY - post a quit message and return 
// 
// 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    int wmId, wmEvent; 
    PAINTSTRUCT ps; 
    HDC hdc; 

    switch (message) 
    { 
    case WM_COMMAND: 
     wmId = LOWORD(wParam); 
     wmEvent = HIWORD(wParam); 
     // Parse the menu selections: 
     switch (wmId) 
     { 
     case IDM_ABOUT: 
      DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 
      break; 
     case IDM_EXIT: 
      DestroyWindow(hWnd); 
      break; 
     default: 
      return DefWindowProc(hWnd, message, wParam, lParam); 
     } 
     break; 
    case WM_PAINT: 
     hdc = BeginPaint(hWnd, &ps); 
     // TODO: Add any drawing code here... 
     EndPaint(hWnd, &ps); 
     break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 
    default: 
     return DefWindowProc(hWnd, message, wParam, lParam); 
    } 
    return 0; 
} 

// Message handler for about box. 
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    UNREFERENCED_PARAMETER(lParam); 
    switch (message) 
    { 
    case WM_INITDIALOG: 
     return (INT_PTR)TRUE; 

    case WM_COMMAND: 
     if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
     { 
      EndDialog(hDlg, LOWORD(wParam)); 
      return (INT_PTR)TRUE; 
     } 
     break; 
    } 
    return (INT_PTR)FALSE; 
} 

void InitD3D(HWND hWnd) 
{ 
    DXGI_SWAP_CHAIN_DESC scd; 

    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); 

    scd.BufferCount = 1; 
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
    scd.OutputWindow = hWnd; 
    scd.SampleDesc.Count = 4; 
    scd.Windowed = true; 

    D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &scd, &swapChain, &device, NULL, &deviceContext); 
} 

它打破了在最後一行,說:「錯誤LNK2019:解析外部符號D3D11CreateDeviceAndSwapChain @ 48 _cdecl InitD3D(HWND結構* __) 「(?InitD3D @@ YAXPAUHWND __ @@@ Z)」,在函數void引用。

我不知道什麼是錯在這裏,#包括工作中來鏈接D3D庫,並且該程序建立或執行不成功它到達那條線。任何幫助,你們可以給予讚賞。謝謝!

+0

這是你得到的唯一錯誤嗎? – Puppy 2012-03-29 04:08:05

+0

那麼,和「1無法解析的外部符號」 – Jack 2012-03-29 23:05:15

回答

4

出於某種原因,如果我省略了x64庫,它就是w獸人。我不知道爲什麼。

1

你有什麼版本的DirectX SDK?最新的SDK可以從here下載。

此外,請確保您鏈接到正確的庫路徑。默認情況下它是

C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86;$(LibraryPath)

我試圖編譯你的代碼(我不得不註釋掉額外#includes是不可用,只好#define一些ID變量,它是一門不能編譯鏈接錯誤。

+0

是我的解決方案屬性,在我的輸入下連接器下的附加依賴?我對這一切都很陌生,而這正是真正搞砸我的環節。在C#中很容易... – Jack 2012-03-27 20:03:31

+0

@Jack:看起來像誰/想教你的是一個無能的狒狒。 – Puppy 2012-03-29 04:04:27

+0

嗯....我認爲我發現的教程(http://www.directxtutorial.com/Tutorial11/B-A/BA5.aspx#still)假設了C++的相對高級別的理解,我沒有。我仍然無法準確確定什麼類型的Visual Studio項目,我應該用這個... – Jack 2012-03-29 22:04:40

1

,想到的第一件事是,這個功能不適用於地鐵應用存在的。如果您使用的是vNext預覽版本,則該功能將不可用。

0

似乎來自舊版SDK的d3d11庫並未定義此GUID。這裏是一種替代方法,你還沒有準備好切換的SDK還...

添加以下幾行代碼(在cpp文件在你需要它)...

const GUID __declspec(selectany) IID_ID3D11Device = { 
    0xdb6f6ddb, 
    0xac77, 
    0x4e88, 
    {0x82, 0x53, 0x81, 0x9d, 0xf9, 0xbb, 0xf1, 0x40} 
}; 
0

嘗試鏈接與DXGUID.LIB

#pragma comment (lib, "dxguid.lib") 

你還沒有提到你正在使用的編譯器的版本。如果您使用的是VS 2012或VS 2013,那麼您需要採取一些額外的步驟來使用舊版DirectX SDK中的D3DX11等舊內容,而不會出現奇怪的錯誤。見Where is the DirectX SDK?。如果您使用的是VS 2010或更高版本,則必須將對DirectX SDK的引用手動添加到您的VC++目錄 - 這不是自動的。

你應該看看最新版本的Direct3D 11教程(Win32桌面版本)MSDN Code Gallery。您還可以找到DirectX Tool Kit以及文章Living without D3DX