0

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

Lucas Hu
  • 159
  • 1
  • 1
  • 7
  • Added [gcc] tag because Cilk is a C extension, and the question is specifically about its use with GCC. – John Bollinger Dec 07 '15 at 20:50
  • @LucasHu [I think this is what you need](https://www.cilkplus.org/build-gcc-cilkplus). – Michi Dec 07 '15 at 20:51
  • It would be helpful if you specified exactly what errors you get. Still, among the plausible problems are that you need to include the cilk runtime library in your link (`-lcilkrts`), and that your copy of GCC is built without Cilk support. – John Bollinger Dec 07 '15 at 20:58
  • @Michi My office computer has intel compiler and can run Cilk code, I am not a computer guy but need to write C code for math research. I thought GCC5+ supports Cilk or Cilkplus flawlessly... no need to build anymore. – Lucas Hu Dec 07 '15 at 21:01
  • @JohnBollinger I updated the result. thanks – Lucas Hu Dec 07 '15 at 21:03
  • 1
    @LucasHu, yours are linker errors, involving Cilk runtime functions. They very likely mean that you need to explicitly include the Cilk runtime library in your link command (or in your compilation command if you do it all in one step). So in addition to `-fcilkplus`, you probably also need `-lcilkrts`. – John Bollinger Dec 07 '15 at 21:07
  • @JohnBollinger -lcilkrts fixes the problem, does that mean that something is wrong about my gcc library? I build this gcc5.2 in MAC. – Lucas Hu Dec 07 '15 at 21:24
  • @LucasHu, no, I don't think anything is wrong with your GCC library. GCC simply differs from the Intel compiler with respect to which functions are available without explicitly linking any libraries. This is of no particular account -- consider that compilers differ on whether the standard math library is linked by default, too. – John Bollinger Dec 07 '15 at 21:49

1 Answers1

1

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.