Jump to content
  • Advertisement
Sign in to follow this  
Lonefox1

automatic include? O_O

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hay guys, just wrote up a TGA loader (cpp and h) and added it to my project in dev cpp, i was always under the inpression you had to then include the cpp or the h in your main cpp, yet with this it seems to do it automatically and i can use the functions from the loader in my main cpp. If i do try to include it i get multiple definition errors... im just rather confused at this @_o. could someone explain whats going on there? cheers si

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You should never include an implementation file, just the header associated with it. It is possible that the file, where you use the TGA related functions, includes other file(s) that in turn might eventually include the header of the said TGA loader.


// ville

Share this post


Link to post
Share on other sites
Unlike AP, I think that you did not try to include the .cpp files, but the .h.

Maybe it is somekind of a "project" thing in dev cpp, that all the files you include to your project gets included to your main?

Anyway, you should have


#ifndef INC_MYFILE_H_
#define INC_MYFILE_H_

...
..

#endif




in your .h files to prevent multiple definition errors.

EDIT:
Oh sorry, you did wonder if you should include your .cpp!
No, as AP/Ville said, you should never do that.

After writing your .h-files like shown above, put

#include "myfile.h" in your "myfile.cpp"-file, as well as in the .cpp file where you might want to use it (like "main.cpp").

Share this post


Link to post
Share on other sites
actually it would seem i was wrong, the file is not including automatically as i cant access the functions, however im now stuck with the mutliple definition problems. heres how ive set it up:

winmain:

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "CGfxOpenGL.h"

*snip*



CGfxOpenGL.h:

#ifndef __GL_COMPONENT
#define __GL_COMPONENT
..
#endif



CGfxOpenGL.cpp:(this is where i need the TGAloader)

#ifdef _WINDOWS
#include <windows.h>
#endif

#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#include "CGfxOpenGL.h"
#include "TGAloader.h" //===========tga loader
// disable implicit float-double casting
#pragma warning(disable:4305)
...



TGAloader.h

#ifndef TGALOADER_H //see if header has been defed yet
#define TGALOADER_H //if not, define it.

#pragma comment(lib, "OpenGL32.lib") //link to opengl32 library
#include <windows.h>
#include <stdio.h>
#include <gl\gl.h>

....

#endif



TGAloader.cpp

#include "TGAloader.h"
..



with that i get quite a few multiple definition errors, i need the tga loader to be included in CGfxOpenGL.cpp, any ideas? im not sure wots gone wrong :S

cheers
si

Share this post


Link to post
Share on other sites
multiple DEFINITION?

Are you defining actual variables, rather than just types, in your header? If you are, don't. If you aren't, post an actual error message :)

Share this post


Link to post
Share on other sites
righto here goes, heres the header and cpp and also the error messages:

TGAloader.h

#include <windows.h>
#include <stdio.h>
#include <gl\gl.h>

typedef struct //structure for holding image data and specs for tga's
{
GLubyte* imageData; //holds all the colour values for the image
GLuint bpp; //holds number of bytes per pix
GLuint width; //width of image
GLuint height; //height of image
GLuint texID; //texure ID for use with glBindTexture.
GLuint type; //data stored in * ImageData (GL_RGB or GL_RGBA)
} Texture;

typedef struct //file header to determine file type (compressed or uncompressed)
{
GLubyte Header[12];
} TGAHeader;

typedef struct
{
GLubyte header[6]; //holds first 6 useful bytes of the file
GLuint bytesPerPixel; //number of BYTES per pixel (3 or 4) rgb or rgba?
GLuint imageSize; //amount of memory needed to hold it. for use with memalloc
GLuint type; // rgb or rgba
GLuint Height; //height of image
GLuint Width; //width
GLuint Bpp; //bits per pix 24 or 32.
}TGA;

TGAHeader tgaheader; //used to store file header info
TGA tga; //stores file information

//here we define some headers so we can tell wot kind of headers are valid

GLubyte uTGAcompare[12] = {0,0,2,0,0,0,0,0,0,0,0,0}; //uncompressed header
GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; //compressed header

//functions:

bool LoadUncompressedTGA(Texture *, char *, FILE *); //load a uncompressed TGA
bool LoadCompressedTGA(Texture *, char *, FILE *); //load a compressed TGA

#endif //__TGALOAD_H__ // end inclusion guard




TGAloader.cpp

#include "TGAloader.h"

//load a TGA
bool LoadTGA(Texture * texture, char * filename) //(pointe to tex struct, string for file name)
{
FILE * fTGA; //declare file pointer
fTGA = fopen(filename, "rb"); //open file for reading

//error checking for file opening
if(fTGA == NULL) //if error
{
//error code
return false;
}

//attempt to read file header (first 12 bytes)
if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0) //fread (buffer, number of bytes, number of times, file)
{
//error stufffs
return false;
}

//now we try determine what type of file it is by comparing the new header to the two hard coded ones

if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader))==0) //if it matches uncompressed memcmp(pointer to buf 1, pointer to buf 2, num of bytes to compare)
{
//load uncompressed
LoadUncompressedTGA(texture, filename, fTGA);
}

//if it matches compressed header:
else if(memcmp(cTGAcompare, &tgaheader, sizeof(tgaheader))==0)
{
//load a compressed
LoadCompressedTGA(texture, filename, fTGA);
}
else
{
//error code here
return false;
}
}

//load uncompressed TGA
bool LoadUncompressedTGA(Texture * texture, char * filename, FILE *fTGA)
{
//atempt to read next 6 bytes
if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)
{
return false;
}

texture->width = tga.header[1] * 256 + tga.header[0]; //calculate height.. umm?
texture->height = tga.header[3] * 256 + tga.header[2]; // umm again
texture->bpp = tga.header[4]; //calculate bpp
tga.Width = texture->width; //copy width to local strcut
tga.Height = texture->height; // ""
tga.Bpp = texture->bpp;

//now we check if height and width pixels are atleast 1 and bpp is either 24 or 32
if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp !=24) && (texture->bpp !=32)))
{
//error
return false;
}

//now we set the type of image, 24 bit is rgb, 32 is rgba
if(texture->bpp==24) //is it 24bpp?
texture->type = GL_RGB; //if so set to gl_rgb
else //if its not it has to be 32... or sumthin has gone terribly wrong :P
texture->type = GL_RGBA; //set type to rgba

//now we calculate BYTES per pixel (8 bytes = 1 bit)
tga.bytesPerPixel = (tga.Bpp / 8); //calc bytes per pix
//now we calc the memory needed
tga.imageSize = (tga.bytesPerPixel * tga.Width * tga.Height);

//we now need the right amount of memory to store the image data. then some error checking to make sure it was allocated
texture->imageData = (GLubyte *)malloc(tga.imageSize);
if(texture->imageData == NULL) //make sure it was allocated ok
{
//error
return false;
}

//here we try to read all the image data, if not error it.
if(fread(texture->imageData, 1, tga.imageSize, fTGA) != tga.imageSize)
{
//error
return false;
}

//tga stores data in BGR format so we need to swap R and B round so its RGB, this uses bit swapping
for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)
{
//1st byte XOR 3rd byte XOR 1st byte XOR 3rd byte
texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=
texture->imageData[cswap] ^= texture->imageData[cswap+2];
}

fclose(fTGA);
return true;
}

bool LoadCompressedTGA(Texture * texture, char * filename, FILE * fTGA)
{
}




errors:
TGAloader.o(.bss+0x0): In function `Z7LoadTGAP7TexturePc':
C:/c++/opengl/texloadtest/TGAloader.cpp:5: multiple definition of `tgaheader'

CGfxOpenGL.o(.bss+0x0):C:/c++/opengl/texloadtest/CGfxOpenGL.cpp:14: first defined here
TGAloader.o(.bss+0x20): In function `Z7LoadTGAP7TexturePc':
C:/c++/opengl/texloadtest/TGAloader.cpp:10: multiple definition of `tga'
CGfxOpenGL.o(.bss+0x20):C:/c++/opengl/texloadtest/CGfxOpenGL.cpp:18: first defined here
TGAloader.o(.data+0x0): In function `Z7LoadTGAP7TexturePc':
C:/c++/opengl/texloadtest/TGAloader.cpp:5: multiple definition of `uTGAcompare'
CGfxOpenGL.o(.data+0x0):C:/c++/opengl/texloadtest/CGfxOpenGL.cpp:14: first defined here
TGAloader.o(.data+0xc): In function `Z7LoadTGAP7TexturePc':
C:/c++/opengl/texloadtest/TGAloader.cpp:7: multiple definition of `cTGAcompare'
CGfxOpenGL.o(.data+0xc):C:/c++/opengl/texloadtest/CGfxOpenGL.cpp:15: first defined here
collect2: ld returned 1 exit status

make.exe: *** [textureloadtest.exe] Error 1

Execution terminated

Share this post


Link to post
Share on other sites
You should move your variable definitions into your .cpp file. If your other .cpp files need access to them, then just declare them as extern in your .h file.

Share this post


Link to post
Share on other sites
righto ive moved the variable definitions to the cpp, compiles fine. now when i try to access the LoadTGA method from the file ive included the tga header in, i get a undeclared function error:

CGfxOpenGL.cpp: In member function `bool CGfxOpenGL::Init()':
CGfxOpenGL.cpp:33: error: `LoadTGA' undeclared (first use this function)
CGfxOpenGL.cpp:33: error: (Each undeclared identifier is reported only once for each function it appears in.)

make.exe: *** [CGfxOpenGL.o] Error 1

im trying to call it with:

LoadTGA(&texture[0], "opengl_logo_un.tga");

is there any special prefix i need to use to call functions from included files?
cheers
Si

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!