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.

Beginners > How do I properly include & access extern const (.h/.c)?

#21104 - aleister - Sun May 23, 2004 10:17 pm

Searching this forum, I came across several threads explaining that data should be placed in a .c-file, not in a header-file. To my pleasant surprise this approach also provided an answer to the problem of running out of memory since it would not automatically try to put all the data I define into the working RAM...

What I can't figure out however, is how to address the data when it is included like this. Upon compiling I keep getting the message that the variables are undeclared (first use in this function).

I used to do it like this: an array containing the different frames of animation for a sprite were located in, for example, gfxsprite0.h, like this:

Code:

const u16 gfxsprite0Data[] = {
   0xFFFF, 0xFFFD, 0x0000, 0xFFFF, etc...etc... 0x0000, 0x0000,};
const u16 gfxsprite0Palette[] = {
   0x7FFF, 0x35B8, 0x0850, etc... etc.. 0x67FF, 0x7FFF,};


In the main c-source, I would include it using #include gfxsprite0.h and copy the desired part of the array to VRAM using:

Code:

memcpy( (u16 *)0x06014000, &gfxsprite0Data[0], 4096 );


(in this example 4096 since I'm working with a 64x64 sprite using 256 colors in mode4)

The above code compiles and works perfectly, but I run into the undeclared variable problem when I attempt to do it the proper way, and change gfxsprite0.h to:

Code:

#ifndef PLAYERGFX
#define PLAYERGFX

extern const u16 gfxsprite0Data[];

#endif


and move the actual data to a file named gfxsprite0.c:

Code:

extern const u16 gfxsprite0Data[] = {
   0xFFFF, 0xFFFD, 0x0000, 0xFFFF, etc...etc... 0x0000, 0x0000,};
extern const u16 gfxsprite0Palette[] = {
   0x7FFF, 0x35B8, 0x0850, etc... etc.. 0x67FF, 0x7FFF,};


I am slowly getting the idea that this may have something to do with the make.bat-file; my make.bat looks like this:

Code:

path=C:\devkitadv\bin
gcc -o maingame.elf maingame.c -lm
objcopy -O binary maingame.elf maingame.gba


For all completeness: I'm compiling this using devkitadvance.

My sincere apologies if this issue has already been explained, but if it has, I'm afraid I couldn't find it while searching through this forum. Thanks ever so much in advance for any help you can offer.

#21113 - BeeWarloc - Mon May 24, 2004 1:09 am

I think this

Code:

extern const u16 gfxsprite0Data[] = {
   0xFFFF, 0xFFFD, 0x0000, 0xFFFF, etc...etc... 0x0000, 0x0000,};
extern const u16 gfxsprite0Palette[] = {
   0x7FFF, 0x35B8, 0x0850, etc... etc.. 0x67FF, 0x7FFF,};


should be this

Code:

const u16 gfxsprite0Data[] = {
   0xFFFF, 0xFFFD, 0x0000, 0xFFFF, etc...etc... 0x0000, 0x0000,};
const u16 gfxsprite0Palette[] = {
   0x7FFF, 0x35B8, 0x0850, etc... etc.. 0x67FF, 0x7FFF,};


Because declaring it extern means that it's not in the source file, but somewhere else. You only should use the extern keyword in the header file, and actually you don't really have to use it extern keyword there either..

#21115 - poslundc - Mon May 24, 2004 1:42 am

You also need to include gfxsprite0.c in the list of files you want gcc to process.

Dan.

#21284 - aleister - Wed May 26, 2004 5:45 pm

Thanks very much; I guess I'd better start learning a bit more about make-files :) At least I now know that that is indeed where the problem lies. Thanks!