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++ > Class initialiser not working in global space.

#19222 - Krakken - Wed Apr 14, 2004 7:23 am

Hi,

For some reason when I declare a class with initialiser parameters outside of a function, whether this be main() or any other, the constructor is not executed.

Example:
Code:

// IMAGINE THE CONSTRUCTOR SETS cValue AS THE INITIAL PARAMETER

// WORKS //
int main() {
  class_object class_name ('A'); // class_name.cValue is set to 'A'

  while (true);
  return 0;
}

// DOES NOT WORK //
class_object class_name ('A'); // class_name.cValue is not set to anything

int main() {
  while (true);
  return 0;
}


Any ideas why?

#19224 - f(DarkAngel) - Wed Apr 14, 2004 11:45 am

In C, globals should be assigned staticaly, in bss. If you want compiler to execute a function at compile time, use macros.
It worked without any problems with the standard (x86) GCC in Linux. It might be a init execution problem of the start up code (Jeff Frohwein?). With a simple logic, your (run-time) program execution starts at main, not global inits.
_________________
death scream...

#19226 - torne - Wed Apr 14, 2004 4:01 pm

Commonly available link scripts (the one in DKA and Jeff's) should execute constructors correctly for globals but you may need to turn on C++ support in the crt0 file. This enables the code that runs the .ctors entries.

#19233 - sajiimori - Wed Apr 14, 2004 9:22 pm

No offense f(DarkAngel), but your post is kinda nonsense. Executing functions at compile time?? This ain't Lisp ya know. :)

#19243 - tepples - Thu Apr 15, 2004 5:52 am

Though it's not as powerful in practice as Lisp's, the C++ preprocessor is said to be Turing complete.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#19255 - f(DarkAngel) - Thu Apr 15, 2004 12:04 pm

sajiimori wrote:
No offense f(DarkAngel), but your post is kinda nonsense. Executing functions at compile time?? This ain't Lisp ya know. :)


#define SOMEVALUE_SET(x) ((x) >> 4)
#define GLOBAL_SOMETHING (*((vu16*)0x4543523412))

GLOBAL_SOMETHING = SOMEVALUE_SET(5);


A macro is what i intended with "complie time function". A macro can be written without violating the mathematical meaning of the "function". Is this non-sense?
_________________
death scream...

#19258 - poslundc - Thu Apr 15, 2004 2:43 pm

Actually, that example is nonsense. You can't place executable code like that outside of a function.

Dan.

#19266 - f(DarkAngel) - Thu Apr 15, 2004 5:06 pm

Of course it's not possible to implement that example outside of a function. It's very normal for some to intentionally miss the true intesion of a sentence, even though there's an explanation in the previous/next sentence; both of my posts.. I have seen this kind in communities i have been, internet either real life, and naturally this community cannot be an exception.

This example has no assertion that the code is inside of a function. It emphasizes that a functionality can be executed by compiler, a reply to the previous post, "Executing functions at compile time??". If it's going to statisfy your insatible ego:

int my_global = SOMEVALUE_SET(5);

is valid in global scope.

The discussions you poslundc and sajiimori have started are off-topic, and this is what is non-sense.

Yes poslundc , i am non-sense, yes i am idiot and ignorant, yes you are the greatest. If you don't have anything to say about the actual purpose, question of this thread, don't reply. Case ended.
_________________
death scream...

#19269 - poslundc - Thu Apr 15, 2004 7:20 pm

f(DarkAngel) wrote:
If you don't have anything to say about the actual purpose, question of this thread, don't reply. Case ended.


I'm sorry, I thought this thread was about initializing objects in global space. I don't know where I got that idea from.

Dan.

#19567 - wintermute - Thu Apr 22, 2004 2:42 am

Krakken wrote:
Hi,

For some reason when I declare a class with initialiser parameters outside of a function, whether this be main() or any other, the constructor is not executed.

Any ideas why?


most GBA crtls don't support global constructors. The only exception is the set built in to devkitARM - http://www.devkit.tk .

Documentation and examples are still being worked on so apologies for a lack of information at present.

Code should be linked using arm-elf-g++ -specs=gba.specs -o <output>.elf <object list>

please feel free to join the mailing list detailed on the about page for any queries you may have.

#19568 - wintermute - Thu Apr 22, 2004 2:45 am

torne wrote:
Commonly available link scripts (the one in DKA and Jeff's) should execute constructors correctly for globals but you may need to turn on C++ support in the crt0 file. This enables the code that runs the .ctors entries.


This is not the case, the CPP support only changes the name of the main function. With the arm-elf target this is not enough to enable global constructors.

#19581 - torne - Thu Apr 22, 2004 11:16 am

The crt0 I use executes the _ctors section just fine.. it claims to be Jeff's. Perhaps someone else modified it.. I forget where I downloaded it from.