Search code examples
c++rllvmrcpp

Calling a c++ function on Rstudio on a MAC and getting (clang: error: unsupported option '-fopenmp')


I know how to code but I really do not know my way around a computer.

I have a program that I have to run for my master thesis. It is a code with multiple collabs and runs perfectly on Linux. However, it is a very complex simulational code and therefore it takes time to run for multiple parameters. I've been using my Linux at the university to run it but would like to run some of it on my personal computer (MAC OS). It works by using the R language to call upon c++ functions as follows (being filename a code on c++).

On a Rstudio script:

Sys.setenv("PKG_CPPFLAGS" = "-fopenmp -DPARALLEL") 
system("rm filename.so") 
system("rm filename.o") 
system ("R CMD SHLIB filename.cpp") 
dyn.load("filename.so")

After system ("R CMD SHLIB filename.cpp") I get error:

clang: error: unsupported option '-fopenmp' make: *** [filename.o] Error 1

I've researched on the subject and found this Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

I've Installed LLVM, yet I do not know how to use it in this case.

How do I use it in this case? Thank you in advance.


Solution

  • "Don't do it that way." Read up on R and Rcpp and use the proper tools (especially for packaging and/or compiling) which should pick up OpenMP where possible. In particular,

    "Just say no" to building the compilation commands by hand unless you know what you are doing with R and have read Writing R Extensions carefully a few times. It can be done, I used to show how in tutorials and workshops (see old slides from 12-15 years ago on my website) but we first moved to package inline which helps here, and later relied on the much better Rcpp Attributes.

    Now, macOS has some extra hurdles in which tools work and which ones don't. The rcpp-devel mailing list may be of help, the default step is otherwise to consult the tutorial by James.

    Edit: And of course if you "just want the above to work" try the obvious step of removing the part causing the error, i.e. use

    Sys.setenv("PKG_CPPFLAGS" = "") 
    

    as your macOS box appears to have a compiler but not OpenMP (which, as I understand it, is the default thanks to some "surprising" default choices at Apple -- see the aforementioned tutorial for installation help.)