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++ > .a library question

#26648 - noaktree - Tue Sep 21, 2004 10:37 pm

Hi everyone - this is my first post.

I have some sprite data:

const u16 SpriteData[] = { ... };

I have stored this data inside of a C++ file
I then compiled the source into a .o file using this:

gcc -c -o Splash.o Splash.cpp

Next I made it into a .a library using archive:

ar rvs Splash.a Splash.o

...ok anyway...now I want to access this data from an app that the .a file is linked to. How do I do this?

I was thinking that I could just declare "extern" variables and access the data this way but it is not working.

Neil
_________________
_Neil

#26650 - noaktree - Tue Sep 21, 2004 10:57 pm

All better now...

I removed the "const" from the original declaration and now it works!

Neil
_________________
_Neil

#26651 - DiscoStew - Tue Sep 21, 2004 11:34 pm

Wouldn't removing the "const" put your array into IWRAM? "Const" usually puts things into ROM since the data doesn't change.
_________________
DS - It's all about DiscoStew

#26659 - Cearn - Wed Sep 22, 2004 9:50 am

I remember having a similar problem a long time ago. As I recall, C++ seems to have a funny way of dealing with extern const data. If you have const data in a cpp file and you want to access it somewhere else you need to ad extern declarations in both the other file and the data file:

Code:

// in foodata.cpp
extern const u16 data[];

const u16 data[]={...};


// code.cpp
extern const u16 data[];
// use as normal

This isn't necessary in C, but it apparently is in C++. Try this instead of removing const, which as DiscoStew said would place it into IWRAM which is not a good place to store a splash image.

#26670 - sajiimori - Wed Sep 22, 2004 7:11 pm

Instead of repeating the extern declaration, put it in a header and include it from both files.

I didn't realize until recently that const in C++ means something different than const in C. (Just when you thought you understood C++!) I guess const definitions are almost like #defines, unless you take their address or declare them extern, which turns them into regular memory objects.

Anybody have a more complete explanation of the differences?

#26688 - torne - Thu Sep 23, 2004 5:47 pm

That is the difference. ;)
In C++, anything defined as const is by default only generated if there is a requirement for it to be generated (used in scope, extern, or address taken).