[En-Nut-Discussion] Class constructor doesn't execute

Krzysztof Sawicki krzysztof.sawicki at mlabs.pl
Mon Dec 5 13:04:18 CET 2016


Hi,

recently I tried some C++ with NutOS and realised that class' constructors are not executed. I did some research and realised that following code 
fixes this issue:

in linker script add:
  	.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

in main() at the beginning add:
	/* Call static constructors */
	asm("bl __libc_init_array");


and anywhere in the code:

	extern "C"
	{
		void *__dso_handle = NULL;
		void _init(void){}
	}


But still no idea how to integrate these above into NutOS. Add to eg. Cortex_Start in cortex_init.c?

-- 
Krzysztof Sawicki
MLabs sp. z o.o.
ul. Kaliska 21
61-131 Poznań

tel. 61 646 84 27

KRS: 0000390306
NIP: 7822533401


More information about the En-Nut-Discussion mailing list