Constructors for global C++ objects should be called automatically, as long
as you're not doing something in the link phase that disrupts things.  My
testing with the program below shows that it seems to work.  Note that if
you fall off the end of main(), the global dtors are called.

0x1c00#global ctor
0x2bfe#local ctor
In main
0x2bfe#local dtor
0x1c00#global dtor

Peter

#include <stdio.h>

class CtorDtor {
  public:
    CtorDtor (const char* id) :
      id_(id)
    {
      printf("%p#%s ctor\r\n", this, id_);
    }
    ~CtorDtor ()
    {
      printf("%p#%s dtor\r\n", this, id_);
    }
  private:
    const char* id_;
};

CtorDtor global("global");

int main ()
{
  CtorDtor local("local");
  printf("In main\r\n");
  return 0;
}

On Wed, Oct 20, 2010 at 9:48 AM, Tobias Baumgartner
<[email protected]>wrote:

> Dear list,
>
> I'm currently trying to port a project (using C++) from mspgcc (3.2.3)
> to mspgcc4. It is a generic C++ library, running (among other systems)
> on top of Contiki (which is a C-based firmware/OS).
>
> As far as I know, constructors of global C++ objects are not called
> automatically, so that the user must call them manually. So far I did
> this successfully via
>   typedef void(*ctor_t)();
>   extern ctor_t __ctors_start;
>   extern ctor_t __ctors_end;
>   ...
>   ctor_t ctor = __ctors_start;
>   while( ctor < __ctors_end )
>   {
>     ctor();
>     ctor = (ctor_t)((void*)ctor + sizeof(void*));
>   }
>
> However, compiling the project with mspgcc4, it doesn't work anymore.
> Printing addresses of ctors_start/end via
>   printf( "Call ctors from %04x to %04x\n",
>     (unsigned int)*__ctors_start,
>     (unsigned int)*__ctors_end );
> produces a higher number for ctors start address than for the end
> (looking in the *.map-file, everything looks fine).
>
> Do I understand sth wrong (so, is the above code incorrect, and it
> worked on the 3.2.3 just "by luck")? Do you have an idea, how I should
> call the constructors?
>
> Or is the information above incomplete - then please let me know!
>
>
> Best,
> Tobias
>
>
> ------------------------------------------------------------------------------
> Nokia and AT&T present the 2010 Calling All Innovators-North America
> contest
> Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
> $10 million total in prizes - $4M cash, 500 devices, nearly $6M in
> marketing
> Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store
> http://p.sf.net/sfu/nokia-dev2dev
> _______________________________________________
> Mspgcc-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mspgcc-users
>
------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to