Re: Class constructor doesn't execute
Krzysztof Sawicki <[email protected]> Thu, 8 Dec 2016 12:53:11 +0100
| Newsgroups | gmane.comp.hardware.microcontrollers.ethernut |
|---|---|
| Organization | Mlabs sp. z o.o. |
| Message-ID | <[email protected]> |
On 07.12.2016 17:19, Uwe Bonnes wrote: >>>>>> "Krzysztof" == Krzysztof Sawicki <[email protected]> writes: > > Krzysztof> On 05.12.2016 14:59, Uwe Bonnes wrote: > >>>>>>> "Krzysztof" == Krzysztof Sawicki <[email protected]> > >>>>>>> writes: > >> > Krzysztof> Hi, recently I tried some C++ with NutOS and realised that > Krzysztof> class' constructors are not executed. I did some research and > Krzysztof> realised that following code fixes this issue: > > Krzysztof> app/cppdemo builds, but I tried such code: > > Does unmodified cppdemo work as expected? If it works, can you explain why > it works? And can you then extend cppdemo in a sensible way to show your > problem? An applicable patch is welcome! Ok, I checked out fresh NutOS from trunk and checked: - app/cppdemo works ok - app/cppdemo with modification: --------------------------------------------------------------------------------- --- cppdemo.cc-orig 2016-12-08 11:57:23.000000000 +0100 +++ cppdemo.cc 2016-12-08 12:12:40.837170830 +0100 @@ -91,7 +91,7 @@ m_value = initValue; } - +Counter counter2; int main(void) { u_long baud = 115200; @@ -104,6 +104,7 @@ Counter counter; counter.print(stream); + counter2.print(stream); for (;;) { char c; --------------------------------------------------------------------------------- shows following output on terminal: C++ Demo on Nut/OS 5.2.4.0 ready. Counter value = 10 Counter value = 0 so, it seems like the constructor for counter2 wasn't executed. Why? No idea, probably of different way of calling constructors: when class is statically created and known at compile time and when class is dynamically created in runtime. After googling for a while and some tests, following patch resolves problem: Index: nut/arch/cm3/cmsis/cortex_init.c =================================================================== --- nut/arch/cm3/cmsis/cortex_init.c (revision 6567) +++ nut/arch/cm3/cmsis/cortex_init.c (working copy) @@ -456,6 +456,10 @@ __enable_irq(); } + +void *__dso_handle = NULL; +void _init(void){} + /*! * \brief CortexM3 Startup. * @@ -465,4 +469,7 @@ { Cortex_MemInit(); Cortex_IntInit(); + + /* Call static constructors */ + asm("bl __libc_init_array"); } Index: nut/arch/cm3/ldscripts/stm32_flash.ld =================================================================== --- nut/arch/cm3/ldscripts/stm32_flash.ld (revision 6567) +++ nut/arch/cm3/ldscripts/stm32_flash.ld (working copy) @@ -55,6 +55,30 @@ *(..exidx* .gnu.linkonce.armexidx.*) __exidx_end = .; } > FLASH0 + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } > FLASH0 + + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } > FLASH0 + + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + } > FLASH0 + _etext = .; .vtable (NOLOAD): -- Krzysztof Sawicki MLabs sp. z o.o. ul. Kaliska 21 61-131 PoznaĆ tel. 61 646 84 27 KRS: 0000390306 NIP: 7822533401 _______________________________________________ http://lists.egnite.de/mailman/listinfo/en-nut-discussion