Thread: Multiple Definition Error

  1. #1
    Registered User
    Join Date
    Dec 2009

    Multiple Definition Error

    Ok so i'm trying to put together a simple graphics program, following the nehe gamedev tutorials. I'm currently trying to load in some tga graphics files to memory.

    I have a header file that is being called by two separate source files. The header file contains a structure as shown below:

    #ifndef __TGA_H__
    #define __TGA_H__
    #include <windows.h>									// Standard windows header
    #include <stdio.h>										// Standard I/O header
    #include <gl\gl.h>										// Header for OpenGL32 library
    #include "texture.h"
    typedef struct
    	GLubyte Header[12];									// TGA File Header
    } TGAHeader;
    typedef struct
    	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
    } TGA;
    TGAHeader tgaheader;									// TGA header
    TGA tga;												// TGA image data
    GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0};	// Uncompressed TGA Header
    GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};	// Compressed TGA Header
    bool LoadUncompressedTGA(Texture *, char *, FILE *);	// Load an Uncompressed file
    bool LoadCompressedTGA(Texture *, char *, FILE *);		// Load a Compressed file
    When I compile everything I get an error saying that TGA and TGAHeader have been defined twice. I understand that this is because the headers are called twice, and the normal solution is to define the objects/functions within one of the sources and only to declare them in the header. Using Extern in the declaration.
    However when I do this I get an error saying that only functions and objects can be declared in this way.

    So my question is how do I deal with these structures? How do I define and Declare them so they can be used in two source files?

  2. #2
    Registered User
    Join Date
    May 2009
    This can NOT be in an normal header file without using one (I use two) more Macros. This way is NOT recommended; but was done in the K&R C days.
    TGAHeader tgaheader;									// TGA header
    TGA tga;												// TGA image data
    Change to this in a header
    extern TGAHeader tgaheader;									// TGA header
    extern TGA tga;										        // TGA image data
    Code to be added not inside a header.
    TGAHeader tgaheader;									// TGA header
    TGA tga;												// TGA image data
    Solution 1: Add this code to the .c file that contains the main function
    Solution 2: Create an globals.c file that holds the above globals and add to your project. (This board seems to like this solution; based on small sample size)
    Solution 3: Link to the old K&R C days way (this is NOT recommend and is harder to get to work for newbies)

    I believe other solutions exist.

    I use solution 1 if small numbers of global variables then change to solution 2 if the number increases.
    I use solution 3 on Rabbit Dynamic C projects because it is NOT a real C Compiler and it works better with it.

    Edit: I just realized this is a C++ thread; normally good C++ OO programmers seem to never seem use global variables as I read on this board.
    I am just learning C++; so, I am not sure what they do instead or if I am wrong on that.

    FYI: It is NOT recommended to use macros that start with two underlines like this "__TGA_H__"; If I recall correctly this is reserved to the Compiler writers.

    Tim S.
    Last edited by stahta01; 04-01-2011 at 08:56 AM. Reason: Added FYI

  3. #3
    Registered User
    Join Date
    Dec 2009
    Hey thanks for the reply, I got this working ok.

    I used the struct declarations, then the extern declarations inside the header. Outside of the header i gave the actual definition of the struct as well as declaring instances of that structure which was what i was missing. Cheers anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM