[英]Linker errors even though I prevent them with #ifndef
I am getting linker errors that suggest I am not using #ifndef and #define. 我收到链接器错误,表明我没有使用#ifndef和#define。
1>TGALoader.obj : error LNK2005: "struct TGA tga" (?tga@@3UTGA@@A) already defined in main.obj 1>TGALoader.obj : error LNK2005: "struct TGAHeader tgaheader" (?tgaheader@@3UTGAHeader@@A) already defined in main.obj 1>TGALoader.obj : error LNK2005: "unsigned char * uTGAcompare" (?uTGAcompare@@3PAEA) already defined in main.obj 1>TGALoader.obj : error LNK2005: "unsigned char * cTGAcompare" (?cTGAcompare@@3PAEA) already defined in main.obj 1>LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs;
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'与使用其他库冲突; use /NODEFAULTLIB:library
使用/ NODEFAULTLIB:库
I have included a header file Texture.h and tga.h from the nehe opengl tutorials into my project. 我已经将一个头文件Texture.h和tga.h从nehe opengl教程中包含到我的项目中。 I have
我有
#ifndef TGAISCOOL
#define TGAISCOOL
#endif
in my tga.h file. 在我的tga.h文件中。 If I include this more than once, I get the errors from the linker that I pasted above.
如果我不止一次地包含它,我会从上面粘贴的链接器中获取错误。 The first two are from texture.h though the situation is the same.
前两个来自texture.h虽然情况相同。
Any ideas on what is wrong? 关于什么是错的任何想法?
The problem is you are putting definitions in your header file instead of declarations. 问题是你在头文件而不是声明中放置定义。
Include guards only work for multiple includes of a single translation unit (ie a source file). 包含保护仅适用于单个翻译单元(即源文件)的多个包含。 If you compile multiple translation units, each one will see the contents of your header file.
如果您编译多个翻译单元,每个翻译单元都会看到您的头文件的内容。
So, instead of putting this definition in your header file: 因此,而不是将此定义放在头文件中:
struct TGA tga;
You want to put this declaration in your header file: 您想将此声明放在头文件中:
/* whatever.h */
extern struct TGA tga;
And then add the definition in a source file: 然后在源文件中添加定义:
/* whatever.c */
#include "whatever.h"
struct TGA ta;
The rule of thumb is that definitions go in source files and declarations go in header files. 经验法则是定义放在源文件中,声明放在头文件中。
You're not doing anything wrong. 你没有做错任何事。 The problem is with the Tga.h file you got from NeHe.
问题出在你从NeHe获得的Tga.h文件中。 This header file defines four objects which means that if you include the file in different translation units the symbols for these will appear multiple times and that is what the linker is complaining about.
此头文件定义了四个对象,这意味着如果您将文件包含在不同的翻译单元中,这些文件的符号将出现多次,这就是链接器所抱怨的内容。
The solution is to move the definitions of these objects into the Tga.cpp file. 解决方案是将这些对象的定义移动到Tga.cpp文件中。
The lines in Tga.h that previously had the definitions should now read 现在应该阅读以前有定义的Tga.h中的行
extern TGAHeader tgaheader;
extern TGA tga;
extern GLubyte uTGAcompare[12];
extern GLubyte cTGAcompare[12];
with the original versions of these lines now in Tga.cpp 这些行的原始版本现在在Tga.cpp中
There's no reason to conclude that #ifndef isn't working properly. 没有理由断定#ifndef无法正常工作。 What the error message is saying is that you have items with the same name defined in multiple translation units (.obj files).
错误消息的含义是您在多个翻译单元(.obj文件)中定义了具有相同名称的项目。 The link process is therefore failing.
因此链接过程失败。
As for how to fix it, we need to see more code. 至于如何解决它,我们需要看到更多的代码。
Change your Tga.H like this: 像这样改变你的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
Add the following lines in your TGALoader.cpp file on top: 在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};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.