Alternate Compiler for Installing R Packages: clan

2019-03-12 23:33发布

问题:

I am trying to install the rJava package on OS X 10.11.6 with R version 3.4.0:

install.packages("rJava", type = "source")

and I get the following error:

clang -o libjri.jnilib Rengine.o jri.o Rcallbacks.o Rinit.o globals.o rjava.o  -dynamiclib -framework JavaVM -fopenmp -L/usr/local/lib -F/Library/Frameworks/R.framework/.. -framework R -lpcre -llzma -lbz2 -lz -licucore -lm -liconv    
clang: error: unsupported option '-fopenmp'
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’

From what I can tell, clang is being used as the compiler, using 'fopenmp' which doesn't appear to be supported by clang. Can anyone see a way around this, potentially forcing a different compiler to be used? (note: I know almost nothing about compilers)

Thanks in advance.

回答1:

This is caused because R 3.4.0 is compiled by CRAN with llvm-4.0.0 (which supports OpenMP), but Apple's fork (installed by default on macOS) does not support OpenMP. There are three solutions

  1. Use the package binaries provided by CRAN, e.g. install.packages(type = "binary").
  2. Install a compiler that does support OpenMP, such as gcc or clang from hombrew, however you will also have to modify variables in your personal makevars file (~/.R/Makevars).
  3. Unset SHLIB_OPENMP_CFLAGS and SHLIB_OPENMP_CXXFLAGS in your ~/.R/Makevars

For 2. you can install the compilers with brew install clang or brew install gcc --without-multilib then you will have to add the compiler path to your ~/.R/Makevars file.

CC=/usr/local/opt/llvm/bin/clang
CXX=/usr/local/opt/llvm/bin/clang++
# Also potentially CXX11 (for C++11 compiler)
CXX11=/usr/local/opt/llvm/bin/clang++

or for gcc use (double check gcc executable exists and is correctly named)

CC=/usr/local/bin/gcc-7
CXX=/usr/local/bin/gcc-7
# Also potentially CXX11 (for C++11 compiler)
CXX11=/usr/local/bin/gcc-7

Alternatively you can install a CRAN Provided LLVM 4.0 and set the Makevars file appropriately.

For 3. you simply need to unset the SHLIB_OPENMP_CFLAGS

SHLIB_OPENMP_CFLAGS=
SHLIB_OPENMP_CXXFLAGS=

For more details see OpenMP support in Writing R Extensions.

Note this error has nothing to do with Java or the rJava package in particular, so ideally the question could be renamed to clang: error: unsupported option '-fopenmp'.



回答2:

Running following in terminal fixes the package download in RStudio too and you can run the Rjava or r-app without any issues.

    brew cask install r-app 
    brew install homebrew/versions/gcc49 --without-multilib 
    sudo chown -R $(whoami):admin /usr/local
    brew link --overwrite --force gcc49
    brew unlink gcc49 && brew link gcc49
    brew install llvm
    mkdir ~/.R; touch ~/.R/Makevars