Re: C++ - Global constructors are not called
Posted: Thu Nov 13, 2014 6:44 pm
jcmvbkbc wrote:joostn wrote:Can anyone help me out? BTW I don't need the C++ standard libraries, I just need to be able to work with my own C++ classes.
Let me look a bit more at our startup sequence...
Ok, there may be straighter ways of doing that, but the easiest that I've found is that: add the following code to your user_main:
(UPD: the original code did wrong constructors array address calculation, thanks to igrr for pointing it. Below is the fixed version.)
Code: Select all
extern void (*__init_array_start)(void);
extern void (*__init_array_end)(void);
static void do_global_ctors(void)
{
void (**p)(void);
for (p = &__init_array_start; p != &__init_array_end; ++p)
(*p)();
}
extern "C" void user_init(void)
{
do_global_ctors();
}
And the following three lines to your linker script:
Code: Select all
--- eagle.app.v6.ld.orig 2014-10-24 16:51:40.000000000 +0300
+++ eagle.app.v6.ld 2014-11-14 02:31:13.658457791 +0300
@@ -96,11 +96,14 @@
*(.gnu.linkonce.e.*)
*(.gnu.version_r)
*(.eh_frame)
+ . = (. + 3) & ~ 3;
/* C++ constructor and destructor tables, properly ordered: */
+ __init_array_start = ABSOLUTE(.);
KEEP (*crtbegin.o(.ctors))
KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors))
+ __init_array_end = ABSOLUTE(.);
KEEP (*crtbegin.o(.dtors))
KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
KEEP (*(SORT(.dtors.*)))