gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Please Help

#8631 - sho - Wed Jul 16, 2003 11:28 pm

I have small problem. Im trying compile this code in devkitadv

Code:

cpixel.h:
#include <gba/gba.h>
#include <gba/screenmode.h>
#ifndef CPIXEL_H
#define CPIXEL_H
class cpixel
{
protected:
   int x, y;

public:
   cpixel(int ax = 0, int ay = 0) : x(ax), y(ay) {}

   void draw();
};
#endif
/***********************************************************/

cpixel.cpp:
#include "cpixel.h"
void cpixel::draw()
{
   ::theBackBuffer[x + y * 240] = RGB(0, 31, 0);
}
/***********************************************************/

main.cpp:
#include "cpixel.h"
int main()
{
   SetMode(MODE_3 | BG2_ENABLE);

   cpixel pixel(10, 100);

   pixel.draw();

   return 0;
}
/***********************************************************/


When im compile this code i recive this erors:
Code:

/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccHgD0P5.o(.data+0x0): multiple definition of `theVideoBuffer'
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccCR5EUV.o(.data+0x0): first defined here
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccHgD0P5.o(.data+0x4): multiple definition of `theBackBuffer'
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccCR5EUV.o(.data+0x4): first defined here
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccHgD0P5.o(.data+0x8): multiple definition of `theScreenPalette'
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccCR5EUV.o(.data+0x8): first defined here
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccHgD0P5.o: In function `PlotPixel(int, int, int, unsigned short)':
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccHgD0P5.o(.text+0x0): multiple definition of `PlotPixel(int, int, int, unsigned short)'
/cygdrive/c/DOCUME~1/sho/USTAWI~1/Temp/ccCR5EUV.o(.text+0x0): first defined here


Im using devkitadv latest version.
What im doing wrong ?
Thx for help.

#8634 - Lord Graga - Thu Jul 17, 2003 12:06 am

Try to find two places in your code that defines/declares stuff like "theVideoBuffer"/etc.
Else, try to check if you included <gba/gba.h> and/or <gba/screenmode.h> twice or more ;)

Best regards, the sleepy nurgling

#8637 - sho - Thu Jul 17, 2003 12:23 am

Im only one time include gba.h and screenmode.h files. Im cheked this files and SetMode and theBackbufer, theVideoBuffer are declared in screenmode.h. No other declaration or definition.

#8641 - night_ - Thu Jul 17, 2003 2:37 am

where do you define "theVideoBuffer", "theBackbuffer"...
Where do you define the Plotpixel function?

#8643 - Nessie - Thu Jul 17, 2003 3:31 am

I wouldn't declare variables and assign them values in your .h file, you can use a #define though. Or you can declare your variable in the .h ( try using the extern keyword), but do the actual assignment in a .cpp file...

Something like:

(my.h)
extern u16 *theBackBuffer;

(some.cpp)
#include "my.h"
theBackbuffer = 0xblahblah;

Now, anywhere you include (my.h), you can have access to theBackBuffer. I probably wouldn't structure it this way, but if you just want to fix your problem and not get any coding style points, I guess do something like this. :)

#8647 - Quirky - Thu Jul 17, 2003 8:45 am

You shouldn't add unecessary #includes to a .h file - includes should really only go in the .c or .cpp that needs them. In your code, you include cpixel.h in 2 places, and this file itself includes gba.h and screenmode.h.

You don't need to include gba.h in the cpixel header as you are not using anything gba specific in that particular file, similarly you should not be including screenmode.h in there either.

These should be included in the cpixel implementation (.cpp) file - but only those headers that define theBackBuffer and the RGB() macro, which may be both gba and screenmode header files or one or the other (though I guess theBackBuffer is defined in gba.h and screenmode has RGB()?). Then in main.cpp, you should include cpixel.h and the header that defines SetMode().

I would recommend getting out of the habit of #include-ing in header files as much as possible. And only include the headers that you actually need in the implementation files.

Having said that, there are times when you will need to #include in a header file. In this case, you should make use of the "#ifndef _whatever_h #define _whatever_h" trick, but don't rely on that so you can include everything everywhere.

#8649 - sho - Thu Jul 17, 2003 10:24 am

Thx for help. Im now include screenmode.h and gba.h in csprite.cpp and include screenmode.h int main.cpp becouse im use in this file SetMode.
Im checked gba.h and screenmode.h and both have prepocesor commands
#ifndef blabla_h
#define blabla_h
code
#endif

but i have stil this same errors.

#8660 - Nessie - Thu Jul 17, 2003 1:42 pm

I used to have the same problems...I figured that the preprocessor command setup you are using would save me from this kind of multiple defintion type bug, but it doesn't really work that way with any of the compilers I've used. Still, using #ifndef guards around your #include's is a good idea for helping compile speed, and as someone already mentioned, not #including files you don't even need is just generally a good idea.

Seriously, if you have variable declarations and assigments in you .h file, you are likely going to have problems. The simplest way to fix this based on what I think you are currently doing is to either use a #define (which is safe in the .h file) or do your assignment in a .cpp file.

#9755 - sajiimori - Sat Aug 16, 2003 5:58 am

When I say:

int x;

I'm saying "create an int right here, right now, called x".

When I say:

extern int x;

I'm saying "there is an int somewhere out there called x".

So, if you put "int x;" in a header file, and include the header file in more than one place, you're creating two ints called x. Some linkers automatically merge them together and only create one int, but that is a deprecated feature of C and should not be used.

In short: headers should never create variables. Declare your variables "extern" in headers, and create the actual variable in a .c or .cpp file.