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 > Solved - Including Data

#130469 - Azenris - Sun Jun 03, 2007 10:32 pm

NDS

Ive been trying to read through a few sites on including data but i feel as though im just confusing myself more. Hoping you can direct me to a simple explanation or point out whats exactly going on.

Im using the makefile included with devkitpro located in the examples\nds\templates\arm9 folder.

I have had my game working but I started reading including data in .h files was bad practice and this is what I was doing (had the tile data in it).

So now i've decided I wanna do it the proper way. Which is something I dont quite get.

===============================================

the file was originally blocks.h and included in the main.cpp.
it included data such as

Code:

const u8 redBlock1[64] =
{
...
};
const u8 yellowBlock1[64] =
{
...
};


so here is where I started trying things. I removed the
#ifndef _INCLUDE_BLOCKS_H
#define _INCLUDE_BLOCKS_H
#endif

and renamed it blocks.cpp and placed it in the source folder.
I then made another file in the include folder, blocks.h which had

Code:

extern const u8 redBlock1[];
extern const u8 yellowBlock1[];
extern const u8 greenBlock1[];
extern const u8 blueBlock1[];
extern const u8 purpleBlock1[];
extern const u8 blackBlock1[];


etc...

I included the blocks.h in the main.cpp.

I attempted to compile assuming the blocks.cpp would be made an object file or something and linked in, but I get the error i have not declared them. redBlock1 not declared. etc

===============================================

what on earth am I doing here :)

P.S Not sure where to post this, I just saw beginner section and thought it was right for me!


Last edited by Azenris on Mon Jun 04, 2007 3:39 pm; edited 1 time in total

#130481 - tepples - Sun Jun 03, 2007 11:42 pm

Azenris wrote:
I attempted to compile assuming the blocks.cpp would be made an object file or something and linked in, but I get the error i have not declared them. redBlock1 not declared. etc

Please paste the exact minimal source code that produces misbehavior, and please paste the exact error messages, without "etc.". Otherwise, we are groping in the dark.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#130485 - Azenris - Mon Jun 04, 2007 12:18 am

My main concern is whether im placing the files in the correct place. I mean should blocks.cpp be in source, with blocks.h in incluce.

This are the errors I get: http://hoholand.myby.co.uk/Error.txt

As for the Code

blocks.cpp located in the source folder (Full here incase http://hoholand.myby.co.uk/blocks.cpp)
Code:

unsigned short int BACKGROUND_COLOUR = BLUE;

const u8 redBlock1[64] =
{
...
};
const u8 yellowBlock1[64] =
{
...
}; 


blocks.h located in the include folder (Full here incase http://hoholand.myby.co.uk/blocks.h)
Code:

#ifndef _INCLUDE_BLOCKS_H
#define _INCLUDE_BLOCKS_H

extern  unsigned short int BACKGROUND_COLOUR;
extern const u8 redBlock1[];
extern const u8 yellowBlock1[];
// plus all the others

#endif


The game did work oringally but I wanted to remove having data in a header by doing this. Is this what you needed ? I dont know what else to include. I don't mind showing it all, just want to understand this.
_________________
My Homebrew Games

#130486 - keldon - Mon Jun 04, 2007 12:32 am

I assume you are including "blocks.h" in "main.cpp" - the file that is giving the errors! And have you also performed make clean?

#130487 - Azenris - Mon Jun 04, 2007 12:47 am

yea its included, whats a make clean though. Or is do you mean by that deleting the build folder and starting fresh.
_________________
My Homebrew Games

#130489 - tepples - Mon Jun 04, 2007 1:36 am

'make clean' means that at the MSYS command prompt, you cd to the folder containing the makefile and then make clean. If you don't understand this, you can do the same thing by deleting all the .o files in the build folder.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#130508 - Cearn - Mon Jun 04, 2007 9:00 am

Note that the errors say "undefined reference", not "variable undeclared". Both blocks.cpp and main.cpp compile fine, it's at the linking stage that it goes wrong.

Try including blocks.h in blocks.cpp as well. If I recall, C++ will consider constant data as 'static' (i.e., local within a file) unless there's an external declaration in the file that has the definition. So

Code:
// blocks.cpp

#include "blocks.h"

const u8 redBlock1[64] =
{
...
};

// etc

Code:
// main.cpp

#include "blocks.h"

// etc

#130521 - Azenris - Mon Jun 04, 2007 3:39 pm

I did need to include blocks.h in the blocks.cpp file. After that it worked fine.

Thanks guys :)
_________________
My Homebrew Games