Friday, February 09, 2007

Nesting C functions on OSX

Frustrated. Why? Because this:
#include <stdio.h>
void outerfunc(void (*innerfunc)(void))
{
  printf("in outermost\n");
  innerfunc();
}
int main(int argc, char * argv)
{
  int x = 0;
  void callback(void)
  {
    printf("in callback\n");
  }
  outerfunc(callback);
  return 0;
}
compiles and runs fine, and this:
#include <stdio.h>
void outerfunc(void (*innerfunc)(void))
{
  printf("in outermost\n");
  innerfunc();
}
int main(int argc, char * argv)
{
  int x = 0;
  void callback(void)
  {
    printf("in callback(x = %d)\n", x);
  }
  outerfunc(callback);
  return 0;
}
gives me this:
% gcc -fnested-functions test3c.c
/usr/bin/ld: Undefined symbols:
___trampoline_setup
collect2: ld returned 1 exit status
The only article I've found about ___trampoline_setup explains the reason for trampolines, and gives a .asm file that defines ___trampoline_setup, but knowing these things hasn't helped me. Either I'm supposed to compile the .asm file in somehow, which I'm not sure how to do, or I have a different problem.

No comments: