Linker errors, although I prevent them from C # ifndef

I get linker errors that suggest that I do not use #ifndef and #define.

1> TGALoader.obj: error LNK2005: "struct TGA tga" (? Tga @@ 3UTGA @@ A) is 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) is already defined in main.obj 1> LINK: warning LNK4098: defaultlib 'LIBCMTD' conflicts with the use of other libraries; usage / NODEFAULTLIB: library

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

#ifndef TGAISCOOL
#define TGAISCOOL
#endif

in my tga.h. file If I enable this more than once, I get errors from the linker that I inserted above. The first two are texture. Although the situation is the same.

Any ideas on what's wrong?

+3
source share
4 answers

You are not doing anything wrong. The problem is the Tga.h file that you received from NeHe. This header file defines four objects, which means that if you include the file in different translation units, the characters for them will be displayed several times, and this is what the linker complains about.

, Tga.cpp.

Tga.h, ,

extern TGAHeader tgaheader;
extern TGA tga;

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

Tga.cpp

+6

, .

(, ). , .

, :

struct TGA tga;

:

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

:

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

struct TGA ta;

, , - .

+7

There is no reason to believe that #ifndef is not working properly. What the error message says is that you have elements with the same name as in several translation units (.obj files). Therefore, the communication process does not work.

To fix this, we need to see more code.

+3
source

Modify your Tga.H as follows:

#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 to your TGALoader.cpp file:

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};
+2
source

Source: https://habr.com/ru/post/1729731/


All Articles