[En-Nut-Discussion] Class constructor doesn't execute
Krzysztof Sawicki
krzysztof.sawicki at mlabs.pl
Tue Dec 6 10:12:36 CET 2016
On 05.12.2016 14:59, Uwe Bonnes wrote:
>>>>>> "Krzysztof" == Krzysztof Sawicki <krzysztof.sawicki at mlabs.pl> 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> in linker script add: .preinit_array : { PROVIDE_HIDDEN
> Krzysztof> (__preinit_array_start = .); KEEP (*(.preinit_array*))
> Krzysztof> PROVIDE_HIDDEN (__preinit_array_end = .); } > FLASH0
>
> Krzysztof> .init_array : { PROVIDE_HIDDEN (__init_array_start =
> Krzysztof> .); KEEP (*(SORT(.init_array.*))) KEEP (*(.init_array*))
> Krzysztof> PROVIDE_HIDDEN (__init_array_end = .); } > FLASH0
>
> Krzysztof> .fini_array : { PROVIDE_HIDDEN (__fini_array_start =
> Krzysztof> .); KEEP (*(SORT(.fini_array.*))) KEEP (*(.fini_array*))
> Krzysztof> PROVIDE_HIDDEN (__fini_array_end = .); } > FLASH0
>
> Krzysztof> in main() at the beginning add: /* Call static constructors
> Krzysztof> */ asm("bl __libc_init_array");
>
>
> Krzysztof> and anywhere in the code:
>
> Krzysztof> extern "C" { void *__dso_handle = NULL; void _init(void){} }
>
>
> Why does app/cppdemo build?
>
> What of these prerequisites is Gnu specific, what is processor specific and
> what is generic. I think anything generic can be added guarded with
> #if defined(__cpluplus)
>
> Any ideas?
app/cppdemo builds, but I tried such code:
...
class A
{
public:
A() {ab=123;}
void print(FILE *stream) {fprintf(stream,"ab=%d\n",ab);}
int ab;
};
A A1;
int main(void) {
u_long baud = 115200;
NutRegisterDevice(&DEV_CONSOLE, 0, 0);
FILE *stream = fopen(DEV_CONSOLE.dev_name, "r+");
_ioctl(_fileno(stream), UART_SETSPEED, &baud);
fprintf(stream, "\n\nC++ Demo on Nut/OS %s ready.\n", NutVersionString());
Counter counter(58);
counter.print(stream);
A A2;
A1.print(stream);
A2.print(stream);
...
and got on terminal:
C++ Demo on Nut/OS 5.2.4.0 ready.
Counter value = 58
ab=0
ab=123
How to solve this problem, any ideas?
greetings
--
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