Compile Cilk plus in GCC5.2.0

364 Views Asked by At

Does anybody know how to compile the following code with Cilk plus in gcc5.2.0 correctly? With gcc -fcilkplus * or g++, I always get errors.

#include <cilk/cilk.h>
#include <assert.h>

int fib(int n) {
    if (n < 2)
    return n;
    int a = cilk_spawn fib(n-1);
    int b = fib(n-2);
    cilk_sync;
    return a + b;
}

int main() {
    int result = fib(30);
    assert(result == 832040);
    return 0;
} 

result:

Undefined symbols for architecture x86_64:
  "___cilkrts_enter_frame_1", referenced from:
      fib(int) in ccY1qrGL.o
  "___cilkrts_enter_frame_fast_1", referenced from:
      __cilk_spn_0 in ccY1qrGL.o
  "___cilkrts_leave_frame", referenced from:
      fib(int) in ccY1qrGL.o
      __cilk_spn_0 in ccY1qrGL.o
  "___cilkrts_rethrow", referenced from:
      fib(int) in ccY1qrGL.o
  "___cilkrts_save_fp_ctrl_state", referenced from:
      fib(int) in ccY1qrGL.o
  "___cilkrts_sync", referenced from:
      fib(int) in ccY1qrGL.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

There are very few topics about this online. thanks

1

There are 1 best solutions below

0
Hansang Bae On

GCC is configured to add -lcilkrts by default to its linker command when -fcilkplus is given by users, but this default behavior can be overridden if there is any platform-specific configuration. I think that is happening on OS X, but it needs to be fixed in my opinion. Anyway, it seems that there is no short-term solution other than adding -lcilkrts as suggested above.