链接器错误,即使我用#ifndef阻止它们

时间:2010-01-23 23:34:07

标签: c++ visual-studio-2008 linker linker-errors

我收到链接器错误,表明我没有使用#ifndef和#define。

  

1> TGALoader.obj:错误LNK2005:   “struct TGA tga”(?tga @@ 3UTGA @@ A)   已在main.obj中定义   1> TGALoader.obj:错误LNK2005:   “struct TGAHeader tgaheader”   (?tgaheader @@ 3UTGAHeader @@ A)已经   在main.obj 1> TGALoader.obj中定义:   错误LNK2005:“unsigned char *   uTGAcompare“(?uTGAcompare @@ 3PAEA)   已在main.obj中定义   1> TGALoader.obj:错误LNK2005:   “unsigned char * cTGAcompare”   (?cTGAcompare @@ 3PAEA)已定义   在main.obj 1> LINK:警告LNK4098:   defaultlib'LIBCMTD'与。冲突   使用其他libs;使用   / NODEFAULTLIB:文库

我已经将一个头文件Texture.h和tga.h从nehe opengl教程中包含到我的 项目。我有

#ifndef TGAISCOOL
#define TGAISCOOL
#endif

在我的tga.h文件中。如果我不止一次地包含它,我会从上面粘贴的链接器中获取错误。前两个来自texture.h,虽然情况相同。

关于什么是错的任何想法?

4 个答案:

答案 0 :(得分:7)

问题是你是在头文件中放置定义而不是声明。

包含防护仅适用于单个翻译单元的多个包含(即源文件)。如果您编译多个翻译单元,每个翻译单元都会看到您的头文件的内容。

因此,不要在头文件中添加定义

struct TGA tga;

您希望在头文件中添加声明

/* whatever.h */
extern struct TGA tga;

然后在源文件中添加定义:

/* whatever.c */
#include "whatever.h"

struct TGA ta;

经验法则是定义放在源文件中,声明放在头文件中。

答案 1 :(得分:6)

你没有做错任何事。问题出在你从NeHe获得的Tga.h文件中。此头文件定义了四个对象,这意味着如果您将文件包含在不同的翻译单元中,这些文件的符号将出现多次,这就是链接器所抱怨的内容。

解决方案是将这些对象的定义移动到Tga.cpp文件中。

以前有定义的Tga.h中的行现在应该是

extern TGAHeader tgaheader;
extern TGA tga;

extern GLubyte uTGAcompare[12];
extern GLubyte cTGAcompare[12];

这些行的原始版本现在在Tga.cpp

答案 2 :(得分:3)

没有理由断定#ifndef无法正常工作。错误消息的含义是您在多个翻译单元(.obj文件)中定义了具有相同名称的项目。因此链接过程失败。

至于如何修复它,我们需要看到更多代码。

答案 3 :(得分:2)

改变你的Tga.H:

#ifndef Tga_H
#define Tga_H
#include "Texture.h"



struct TGAHeader
{
    GLubyte Header[12];                                 // TGA File Header
} ;


struct TGA
{
    GLubyte     header[6];                              // First 6 Useful Bytes From The Header
    GLuint      bytesPerPixel;                          // Holds Number Of Bytes Per Pixel Used In The TGA File
    GLuint      imageSize;                              // Used To Store The Image Size When Setting Aside Ram
    GLuint      temp;                                   // Temporary Variable
    GLuint      type;   
    GLuint      Height;                                 //Height of Image
    GLuint      Width;                                  //Width ofImage
    GLuint      Bpp;                                    // Bits Per Pixel
} ;


extern  TGAHeader tgaheader;                                    // TGA header
extern  TGA tga;                                                // TGA image data



extern GLubyte uTGAcompare[12]; // Uncompressed TGA Header
extern GLubyte cTGAcompare[12]; // Compressed TGA Header
bool LoadTGA(Texture * , char * );
bool LoadUncompressedTGA(Texture *, char *, FILE *);    // Load an Uncompressed file
bool LoadCompressedTGA(Texture *, char *, FILE *);      // Load a Compressed file

#endif

在TGALoader.cpp文件中添加以下行:

TGAHeader tgaheader;
TGA tga;
GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0};
GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};
相关问题