2015-10-14 113 views
0

我试图编译这个:链接使用cmake

#define COBJMACROS 

#include <windows.h> 
#include <d3d10.h> 
#include <d3dx10.h> 

HWND     g_hWnd = NULL; 
ID3D10Device*   g_pd3dDevice = NULL; 
IDXGISwapChain*   g_pSwapChain = NULL; 
ID3D10RenderTargetView* g_pRenderTargetView = NULL; 

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    switch(uMsg) { 
    case WM_CLOSE: 
     DestroyWindow(hwnd); 
    break; 
    case WM_DESTROY: 
     PostQuitMessage(0); 
    break; 
    default: 
     return DefWindowProc(hwnd, uMsg, wParam, lParam); 
    } 

    return 0; 
} 

const char lpszClassName[] = "TutorialWindowClass"; 

HRESULT InitWindow(HINSTANCE hInstance,int nCmdShow) 
{ 
    HWND hwnd; 
    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(NULL, IDI_APPLICATION);; 
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); 
    wcex.lpszMenuName = NULL; 
    wcex.lpszClassName = lpszClassName; 
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    if(!RegisterClassEx(&wcex)) 
     return E_FAIL; 

    g_hWnd = CreateWindow(
     lpszClassName, 
     "DX10 TUT0: CreateWindow", 
     WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, CW_USEDEFAULT, // starting coordinates, Windows decide 
     640, 480, // window size 
     NULL, 
     NULL, 
     hInstance, 
     NULL 
    ); 

    if(!g_hWnd) 
     return E_FAIL; 

    ShowWindow(hwnd, nCmdShow); 

    return S_OK; 
} 

HRESULT InitDevice() 
{ 
    HRESULT hr = S_OK; 
    RECT rc; 
    UINT width; 
    UINT height; 
    DXGI_SWAP_CHAIN_DESC sd; 
    ID3D10Resource* pBackBuffer; 
    D3D10_VIEWPORT vp; 

    GetClientRect(g_hWnd, &rc); 
    width = rc.right - rc.left; 
    height = rc.bottom - rc.top; 

    ZeroMemory(&sd, sizeof(sd)); 
    sd.BufferCount = 1; 
    sd.BufferDesc.Width = width; 
    sd.BufferDesc.Height = height; 
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 
    sd.BufferDesc.RefreshRate.Numerator = 60; 
    sd.BufferDesc.RefreshRate.Denominator = 1; 
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
    sd.OutputWindow = g_hWnd; 
    sd.SampleDesc.Count = 1; 
    sd.SampleDesc.Quality = 0; 
    sd.Windowed = TRUE; 

    hr = D3D10CreateDeviceAndSwapChain(
     NULL, 
     D3D10_DRIVER_TYPE_HARDWARE, 
     NULL, 
     0, 
     D3D10_SDK_VERSION, 
     &sd, 
     &g_pSwapChain, 
     &g_pd3dDevice 
    ); 

    if (FAILED(hr)) 
     return hr; 


    hr = IDXGISwapChain_GetBuffer(g_pSwapChain, 0, &IID_ID3D10Texture2D, (LPVOID*)&pBackBuffer); 

    if(FAILED(hr)) 
     return hr; 

    hr = ID3D10Device_CreateRenderTargetView(g_pd3dDevice, pBackBuffer, NULL, &g_pRenderTargetView); 

    ID3D10Resource_Release(pBackBuffer); 

    if(FAILED(hr)) 
     return hr; 

    ID3D10Device_OMSetRenderTargets(g_pd3dDevice, 1, &g_pRenderTargetView, NULL); 

    // Setup the viewport 

    vp.Width = width; 
    vp.Height = height; 
    vp.MinDepth = 0.0f; 
    vp.MaxDepth = 1.0f; 
    vp.TopLeftX = 0; 
    vp.TopLeftY = 0; 

    ID3D10Device_RSSetViewports(g_pd3dDevice, 1, &vp); 

    return S_OK; 

} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    MSG msg; 

    if(FAILED(InitWindow(hInstance, nCmdShow))) 
     return 0; 


    // Main message loop 

    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 

    return (int)msg.wParam; 
} 

它是一个C代码(不是C++)。它应该使用DirectX 10创建direct3d设备。我正在使用cmake构建我的构建系统。这里是我的CMakeLists文件:

cmake_minimum_required (VERSION 2.6) 
project (DXTUTS C) 

include_directories("C:/Program Files (x86)/Microsoft DirectX SDK (August 2009)/Include") 
link_directories("C:/Program Files (x86)/Microsoft DirectX SDK (August 2009)/Lib/x86") 

add_executable(tut1 WIN32 tut1.c) 
target_link_libraries(tut1 d3d10 d3dx10) 

然而,当我尝试编译,我得到这个连接错误:

[ 50%] Linking C executable tut1.exe 
tut1.c.obj : error LNK2001: unresolved external symbol IID_ID3D10Texture2D 
tut1.c.obj : error LNK2019: unresolved external symbol D3D10CreateDeviceAndSwapC 
hain referenced in function InitDevice 
tut1.exe : fatal error LNK1120: 2 unresolved externals 
LINK Pass 1 failed. with 1120 
NMAKE : fatal error U1077: '"C:\Program Files (x86)\CMake\bin\cmake.exe"' : return code '0xffffffff' 
Stop. 
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64\nmake.exe"' : return code '0x2' 
Stop. 
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\Bin\amd64\nmake.exe"' : return code '0x2' 
Stop. 

我怎样才能成功链接使用DirectX SDK中该C代码?

回答

0

具体问题:

  1. 您指向Lib/x86库目录,但对编译x64体系结构(我可以通过路径看它...Bin\amd64\nmake.exe)。您应该删除CMake缓存(在cmake-gui:菜单“文件” - >“删除缓存”)并重新配置为Visual Studio 10 2010,而不是Visual Studio 10 2010 Win64。或者,您可以将链接器指向Lib/x64目录。

  2. 你缺少dxguid库:

    target_link_libraries(tut1 d3d10 d3dx10 dxguid) 
    

奖励:

  • 没有在2015年学习的DirectX 10点它已经过时了。学习DirectX 11:它是具有更多功能的相同API。

  • DirectX SDK已过时,并且它的使用不受欢迎(由Microsoft提供)。最新版本于2010年6月发布,您甚至可以使用旧版本(2009年8月)。从版本8起,Microsoft已将DirectX SDK与Windows SDK合并。有关详细信息,请参阅this answer

  • 2015年在使用Visual Studio 2010没有意义。它已近6年历史,不支持许多现代C和C++功能(这意味着您没有机会学习它们)。使用Visual Studio 2015. Community edition对于非商业用途是免费的。您将获得完整的套件和Windows SDK(安装期间不要忘记标记“C++开发工具”)。

  • 使用跨平台构建系统(如CMake for DirectX项目)没有意义。无论如何,DirectX仅适用于Windows平台。

  • +0

    谢谢,这解决了我的问题。奇怪的是,在我阅读的任何教程中,dxguid库如何从未被提及过。此外,谢谢奖金信息,我不知道这些点。 – yasar