How do I get the GCC __attribute__ ((constructor)) under OSX?

advertisements
extern void MyInitFunc(void) __attribute__ ((constructor));
extern void MyTermFunc(void) __attribute__ ((destructor));

void MyInitFunc(void)
{
  printf("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n");
}

void MyTermFunc(void)
{
}

I put this in a .c file which is present in the main application (not a library or framework). It doesn't get called as per the GCC docs. Am I missing something important? This is on XCode 3.2 on Snow Leopard.


(a) Your code works for me compiling and running on SnowLeopard in Xcode 3.2.

(b) I'm not sure when stdout is guaranteed to have been set up. You're running code before main. Why not update a global variable here, then print it out in main to see if your code ran.