Forums

Global c++ objects

Started by versluisd December 20, 2006
I've got a problem.. somehow my global object don't seem te be
initialized, or even working...

I've got an object from class LCD. When I do

int main(..){
...
LCD lcd(...,...);
lcd.puts("..");
}

this works fine.. but when I declare lcd outside of main, and use it
inside main or any other function, it doesn't work..
compiling/linking doesn't give any errors either... Like this:

LCD lcd(..,..);

int main(..){
lcd.puts("....");
}

I tried doing it like this:

LCD *lcd;

int main(..){
lcd= new LCD(..,..);
}

but that gives me a linking error:
undefined reference to 'operator new(unsigned long)'

Could anyone shed some light on the topic?
Thanks...

An Engineer's Guide to the LPC2100 Series

Hi,

Looks like global ctors are not called when your LCD object
is declared outside of main. It is most likely a linker
problem. What tools are you using?

elektrknight
----------------------
Embedded IDE and BASIC Compiler for ARM based microcontrollers
http://www.hbbrbasic.com/

--- In l..., "versluisd" wrote:
>
> I've got a problem.. somehow my global object don't seem te be
> initialized, or even working...
>
> I've got an object from class LCD. When I do
>
> int main(..){
> ...
> LCD lcd(...,...);
> lcd.puts("..");
> }
>
> this works fine.. but when I declare lcd outside of main, and use it
> inside main or any other function, it doesn't work..
> compiling/linking doesn't give any errors either... Like this:
>
> LCD lcd(..,..);
>
> int main(..){
> lcd.puts("....");
> }
>
> I tried doing it like this:
>
> LCD *lcd;
>
> int main(..){
> lcd= new LCD(..,..);
> }
>
> but that gives me a linking error:
> undefined reference to 'operator new(unsigned long)'
>
> Could anyone shed some light on the topic?
> Thanks...
>
Oh right, I forgot to mention.. I'm using the ARM gnu-toolchain..
WinARM in fact.. i've got version 4.1.1.. Is it necessary to post the
Makefile i'm using? It's rather large ^^

--- In l..., "elektrknight"
wrote:
>
> Hi,
>
> Looks like global ctors are not called when your LCD object
> is declared outside of main. It is most likely a linker
> problem. What tools are you using?
>
> elektrknight
> ----------------------
> Embedded IDE and BASIC Compiler for ARM based microcontrollers
> http://www.hbbrbasic.com/
> --- In l..., "versluisd" wrote:
> >
> > I've got a problem.. somehow my global object don't seem te be
> > initialized, or even working...
> >
> > I've got an object from class LCD. When I do
> >
> > int main(..){
> > ...
> > LCD lcd(...,...);
> > lcd.puts("..");
> > }
> >
> > this works fine.. but when I declare lcd outside of main, and use
it
> > inside main or any other function, it doesn't work..
> > compiling/linking doesn't give any errors either... Like this:
> >
> > LCD lcd(..,..);
> >
> > int main(..){
> > lcd.puts("....");
> > }
> >
> > I tried doing it like this:
> >
> > LCD *lcd;
> >
> > int main(..){
> > lcd= new LCD(..,..);
> > }
> >
> > but that gives me a linking error:
> > undefined reference to 'operator new(unsigned long)'
> >
> > Could anyone shed some light on the topic?
> > Thanks...
>
Are you using custom linker script? If not, have you made any
modifications to the linker script that came with the gcc?
What is the extension of your source files?

No, do not post entire Makefile. If you could capture the output
messages when you compile and post lines that show command used
to compile one of the source files (the one with the main() would be
best). Post also the last command which links your executable.

elektrknight
----------------------
Embedded IDE and BASIC Compiler for ARM based microcontrollers
http://www.hbbrbasic.com/

--- In l..., "versluisd" wrote:
>
> Oh right, I forgot to mention.. I'm using the ARM gnu-toolchain..
> WinARM in fact.. i've got version 4.1.1.. Is it necessary to post the
> Makefile i'm using? It's rather large ^^
>
> --- In l..., "elektrknight"
> wrote:
> >
> > Hi,
> >
> > Looks like global ctors are not called when your LCD object
> > is declared outside of main. It is most likely a linker
> > problem. What tools are you using?
> >
> > elektrknight
> > ----------------------
> > Embedded IDE and BASIC Compiler for ARM based microcontrollers
> > http://www.hbbrbasic.com/
> >
> >
> >
> >
> > --- In l..., "versluisd" wrote:
> > >
> > > I've got a problem.. somehow my global object don't seem te be
> > > initialized, or even working...
> > >
> > > I've got an object from class LCD. When I do
> > >
> > > int main(..){
> > > ...
> > > LCD lcd(...,...);
> > > lcd.puts("..");
> > > }
> > >
> > > this works fine.. but when I declare lcd outside of main, and use
> it
> > > inside main or any other function, it doesn't work..
> > > compiling/linking doesn't give any errors either... Like this:
> > >
> > > LCD lcd(..,..);
> > >
> > > int main(..){
> > > lcd.puts("....");
> > > }
> > >
> > > I tried doing it like this:
> > >
> > > LCD *lcd;
> > >
> > > int main(..){
> > > lcd= new LCD(..,..);
> > > }
> > >
> > > but that gives me a linking error:
> > > undefined reference to 'operator new(unsigned long)'
> > >
> > > Could anyone shed some light on the topic?
> > > Thanks...
> > >
>
--- In l..., "versluisd" wrote:
>
> I've got a problem.. somehow my global object don't seem te be
> initialized, or even working...
>
...
>
> compiling/linking doesn't give any errors either... Like this:
...
> Could anyone shed some light on the topic?
> Thanks...
>

I am using GNU tools too. In my code I am calling global ctors in the
startup file just before branch to main(). I am using custom startup
file and linker script. In the linker script I have the following

__ctors_start__ = .;
KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors))
__ctors_end__ = .;

And in the startup.s

/* Call constructors */
ldr r0, =__ctors_start__
ldr r1, =__ctors_end__
ctor_loop:
cmp r0, r1
beq ctor_end
ldr r2, [r0], #+4
stmfd sp!, {r0-r1}
mov lr, pc
mov pc, r2
ldmfd sp!, {r0-r1}
b ctor_loop
ctor_end:
Hope this helps.
Wonderful thank you! I got it working.. I think the assembly part in
the startup file was missing.. The linking file did have the ctors
part :) Anyways, many thanks!

Dani

--- In l..., "Zdravko" wrote:
>
> --- In l..., "versluisd" wrote:
> >
> > I've got a problem.. somehow my global object don't seem te be
> > initialized, or even working...
> >
> ...
> >
> > compiling/linking doesn't give any errors either... Like this:
> ...
> > Could anyone shed some light on the topic?
> > Thanks...
> > I am using GNU tools too. In my code I am calling global ctors in
the
> startup file just before branch to main(). I am using custom startup
> file and linker script. In the linker script I have the following
>
> __ctors_start__ = .;
> KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors))
> __ctors_end__ = .;
>
> And in the startup.s
>
> /* Call constructors */
> ldr r0, =__ctors_start__
> ldr r1, =__ctors_end__
> ctor_loop:
> cmp r0, r1
> beq ctor_end
> ldr r2, [r0], #+4
> stmfd sp!, {r0-r1}
> mov lr, pc
> mov pc, r2
> ldmfd sp!, {r0-r1}
> b ctor_loop
> ctor_end:
> Hope this helps.
>