Windows SDK 中 DirectX 的 C++ 链接器问题

标签 c++ visual-studio-2012 linker directx

我正在为 Windows 7 和 8 编写游戏。我一直在使用 VS2012。所以基本上我正在尝试了解 Windows SDK。 d3dX10math 已被替换,我已经接受了这一点,并且在必要时包含了 DirectXMath.h。

问题通常出在链接器上。它没有链接到必要的库。它们就在外部依赖项中(我假设旧的 d3dx10math.lib 已被这些 .inl 文件替换?)。我找不到等效的 .lib 文件,所以这就是我的假设。

它只是不想实际链接到它们。我在网上搜索了高低,似乎没有人问过这个问题。我觉得我错过了什么?

#ifndef _CDEVICE_H_
#define _CDEVICE_H_

//#include <vector>

#pragma once
//#pragma comment(lib, "dxgi.lib")
//#pragma comment(lib, "d3d11.lib")

#include <dxgi.h>
#include <d3d11.h>
#include <DirectXMath.h>

using namespace DirectX;

static class cDevice
{
public:
    cDevice();
    cDevice(const cDevice&);
    ~cDevice();
private:
    static ID3D11Device* device;
    static XMFLOAT2 float2;
} device;

#endif

然后我得到这个链接器错误

1>cDevice.obj : error LNK2001: unresolved external symbol "private: static struct DirectX::XMFLOAT2 cDevice::float2" (?float2@cDevice@@0UXMFLOAT2@DirectX@@A)

最佳答案

static XMFLOAT2 float2; 作为类成员在您的 header 中要求您的项目中有一个 .cpp 文件(比如 CDevice.cpp),该文件具有 XMFLOAT2 Device::float2( 0, 0 );——或者你希望在其中实际声明变量的任何初始值。这使得 CDevice::float2 成为该类的静态成员变量。您已经声明您的 CDevice 有一个 static ID3D11Device* 设备;,这同样需要一个包含 ID3D11Device* CDevice::device = nullptr; 的 .cpp 文件来链接.

此外,不要在标题中放置 using 命名空间语句。它们在 .cpp 文件中很棒,但在 header 中您应该坚持使用全名,例如:DirectX::XMFLOAT2

像这样的.h:

static class cDevice
{
public:
    cDevice();
    cDevice(const cDevice&);
    ~cDevice();
private:
    static ID3D11Device* device;
    static DirectX::XMFLOAT2 float2;
} device;

需要这样的 .cpp 文件:

#include "CDevice.h" using namespace DirectX;
using namespace DirectX;

ID3D11Device* CDevice::device = nullptr;
XMFLOAT2 CDevice::float2(0, 0);

而且,如果您曾将那个 .h 包含在多个 .cpp 文件中,您会发现每个文件中都有自己不相关的 CDevice 拷贝。

我不知道你为什么要这样做,因为这在设计上毫无意义(你应该在你的类 CDevice 中有非静态变量,或者你应该使用 namespace CDevice 代替,因为您实际上并没有使用对象封装,或者可能创建一个单例)。您可能应该阅读 C++ 入门以了解这些基础知识,因为它与 DirectX 毫无关系,而且您似乎对(公认的重载)关键字 static 的使用感到非常困惑>.

我强烈建议您看看使用 SimpleMath DirectX Tool Kit 中 DirectXMath 的包装器,实际上还有 DirectX 工具包的其余部分。

我还建议转到 VS 2013 Community Edition而不是 VS 2012(适用于 Windows 桌面的 Express?),并使用 Direct3D Win32 Game支持 Windows 7 的模板。

关于Windows SDK 中 DirectX 的 C++ 链接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110138/

相关文章:

asp.net - 在 Visual Studio 中更改 asp.net 项目的输出目录

c# - ShowDialog() 不在主窗体之上显示窗体

c++ - 删除元组成员的复制构造函数导致错误

c++ - 如何避免两步初始化

c++ - 将单个轮廓点拆分到它的 HSV channel 中以执行其他操作

c++ - 多个链接 C++ 文件

c++ - CUDA 5.0错误LNK2001 : unresolved external symbol for cuda methods

c++ - 如何适本地将指针设置为新结构?

git - .gitignore 被 Visual Studio 忽略了吗?

c++ - boost 静态链接 boost.locale 错误