标头保护/翻译单元问题

2024-02-13

我的印象是标头保护解决了重新定义的问题。我收到链接器错误,表明 .obj 文件中存在重新定义。这是我要包含的标头,问题在于所有全局声明的重新定义。

#ifndef DIRECT3D_H
#define DIRECT3D_H

// global declarations
ID3D10Device* device;
ID3D10Buffer* pBuffer;
ID3D10Buffer* iBuffer;    // the pointer to the index buffer
ID3D10RenderTargetView* rtv;    // the pointer to the render target view
ID3D10DepthStencilView* dsv;    // the pointer to the depth stencil view
IDXGISwapChain* swapchain;    // the pointer to the swap chain class
ID3D10Effect* pEffect;
ID3D10EffectTechnique* pTechnique;
ID3D10EffectPass* pPass;
ID3D10InputLayout* pVertexLayout;
ID3D10EffectMatrixVariable* pTransform;    // the pointer to the effect variable interface
D3D10_PASS_DESC PassDesc;

// function prototypes
void initD3D(HWND hWnd);
void render_frame();
void init_pipeline();
void cleanD3D();
void Init();

#endif

假设这个头文件名为 3DClass.h。它包含在 3DClass.cpp 中。它还包含在另一个文件中 - 主游戏循环。现在,我意识到当存在多个翻译单元时,头文件可能会出现问题,但我不太明白为什么这不起作用,我只将标头包含在一个文件中以及相应的源文件中。这不应该好吗?


标头防护解决了两次包含相同标头或包含的隐藏递归问题,而不是双重定义。

如果您在不同的翻译单元中包含相同的标头,标头防护将无济于事。

解决办法是never在头文件中声明变量。如果需要共享变量,请使用extern标题中的关键字,并在中声明实际变量one翻译单元的数量。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

标头保护/翻译单元问题 的相关文章

随机推荐