Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker errors even though I prevent them with #ifndef

I am getting linker errors that suggest I am not using #ifndef and #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; use /NODEFAULTLIB:library

I have included a header file Texture.h and tga.h from the nehe opengl tutorials into my project. I have

#ifndef TGAISCOOL
#define TGAISCOOL
#endif

in my tga.h file. 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.

Any ideas on what is wrong?

like image 836
bobber205 Avatar asked Dec 02 '22 06:12

bobber205


1 Answers

You're not doing anything wrong. The problem is with the Tga.h file you got from NeHe. 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.

The lines in Tga.h that previously had the definitions should now read

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

like image 165
Troubadour Avatar answered Dec 09 '22 10:12

Troubadour