How to get GCC __attribute__ ((constructor)) to work on OSX?

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 that is present in the main application (and not in the library or framework). It is not called according to GCC documents. Did I miss something important? This is on Xcode 3.2 on Snow Leopard.

0
source share
2 answers

(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 be configured. You run the code before the main one. Why not update the global variable here, and then print it in the main file to find out if your code is working.

+3
source

, Snow Leopard, .

, MyInitFunc , , C89, .

, C .ctors .dtors . , , __do_global_ctors, libgcc.

, gcc , Apple - -, gcc .

cc -v ... , collect2 ld .

+9

All Articles