2013-06-12 68 views
3

我一直在嘗試將DirectX用於一般遊戲編程,並且一直遵循微軟網站上的教程(http://msdn.microsoft.com/en-us/library/windows/apps/hh452790.aspx)。但是,每當我嘗試做任何事情時,它都會吐出運行時錯誤。這裏是我的代碼:Extern「C」錯誤DirectX

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

#include<iostream> 
#include<d2d1.h> 
#include<Windows.h> 


using namespace std; 

int main() 
{ 
    HWND hwnd; 
    ID2D1Factory* pD2DFactory = NULL; 
    HRESULT hr = D2D1CreateFactory(
     D2D1_FACTORY_TYPE_SINGLE_THREADED, 
     &pD2DFactory 
     ); 

    //Obtain the size of the drawing 
    RECT rc; 
    GetClientRect(hwnd, &rc); 
    //Create a Direct2D rendertarget 
    ID2D1HwndRenderTarget* pRT = NULL; 
    HRESULT hr2 = pD2DFactory->CreateHwndRenderTarget(
     D2D1::RenderTargetProperties(), 
     D2D1::HwndRenderTargetProperties(
      hwnd, 
      D2D1::SizeU(
      rc.right - rc.left, 
      rc.bottom - rc.top) 
      ), 
      &pRT 
     ); 
    cout << "TEST" << endl; 
} 

這裏就是我得到的錯誤:

 1>------ Build started: Project: Game, Configuration: Debug Win32 ------ 
1> Main.cpp 
1>c:\users\will\documents\visual studio 2010\projects\game\game\main.cpp(21): warning C4700: uninitialized local variable 'hwnd' used 
1>Main.obj : error LNK2028: unresolved token (0A00036A) "extern "C" int __stdcall GetClientRect(struct HWND__ *,struct tagRECT *)" ([email protected]@$$J18YGHPAUHWND__@@[email protected]@@Z) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ) 
1>Main.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetClientRect(struct HWND__ *,struct tagRECT *)" ([email protected]@[email protected]@[email protected]@@Z) referenced in function "int __cdecl main(void)" ([email protected]@$$HYAHXZ) 
1>C:\Users\Will\Documents\Visual Studio 2010\Projects\Game\Debug\Game.exe : fatal error LNK1120: 2 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

這些是鏈接器錯誤,而不是運行時錯誤。 – chris

+0

奇怪的是,它會這樣做,但嘗試鏈接到'user32.lib',儘管它通常應該是。 – chris

回答

0

你缺少與您所使用的連接庫(或者您使用的庫的代碼需要它):

在這種情況下,GetClientRect缺少這樣

Right-Click on Project -> Linker -> Input -> Add Dependencies 
Add "User32.lib" to what is there. 

在類似的情況下,尋找福在MSDN上你可以看到所需的庫名(如GetClientRect的here)。

+0

當我右鍵點擊該項目,沒有一個鏈接器選項...編輯:找到它。 – Charsmud

+0

下的「其他依賴項」?編輯:好的,謝謝! – Charsmud

+0

是的,添加依賴關係...對不起,錯過了...將編輯。 – collaborator