Clang C Block : block function definition after call

251 views Asked by At

I'm trying to port some GCC nested function to clang. As gcc nested function is not supported in clang, i need to use c-block instead.

But i want to have the block definition after the call to it. (I need this order because code is generated from MACRO and i can not arrange this order)

So in gcc i have this pseudo code :

foo(){
  auto void bar (void);
  ...
  bar()
  ...
  void bar(void) {
    ...some stuff
  }
}

You i can do this in C-block clang function ?

This code works fine

int main() {
  void (^hello)(void);

  hello = ^(void){
    printf("Hello, block!\n");
  };

  hello();
  return 0;
}

But the following code

int main() {
  void (^hello)(void);

  hello();

  hello = ^(void){
    printf("Hello, block!\n");
  };

  return 0;
}

failed with an segfault.

1

There are 1 answers

0
3Dave On

In your second example, hello has not been defined before you call it, so it is an undefined symbol. You have to tell the compiler what something is before you can use it.

In your pseudocode, a function prototype preceeds everything, which gets around the error by telling the compiler "this will be defined later on."