rJava linker error licuuc with Anaconda & fopenmp

2019-01-12 05:29发布

问题:

I want to install rJava on macOS Sierra 10.12.4. My current Java version is Java version: 1.8.0_131, according to R CMD javareconf|grep version. I have installed Java with Homebrew's cask.

My Java runs the following

$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

My R version is

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin11.4.2 (64-bit)

R Java config runs such that

$ R CMD javareconf
Java interpreter : /usr/bin/java
Java version     : 1.8.0_131
Java home path   : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Java compiler    : /usr/bin/javac
Java headers gen.: /usr/bin/javah
Java archive tool: /usr/bin/jar
Non-system Java on macOS

trying to compile and link a JNI program 
detected JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
During startup - Warning messages:
1: Setting LC_COLLATE failed, using "C" 
2: Setting LC_TIME failed, using "C" 
3: Setting LC_MESSAGES failed, using "C" 
4: Setting LC_MONETARY failed, using "C" 
clang -I/Volumes/osx/201705_anaconda/anaconda/lib/R/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include/darwin -I/Volumes/osx/201705_anaconda/anaconda/include    -fPIC  -I/Volumes/osx/201705_anaconda/anaconda/include  -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Volumes/osx/201705_anaconda/anaconda/lib/R/lib -arch x86_64 -L/Volumes/osx/201705_anaconda/anaconda/lib -lgfortran -L/Volumes/osx/201705_anaconda/anaconda -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server -ljvm -L/Volumes/osx/201705_anaconda/anaconda/lib/R/lib -lR -lintl -liconv -lc -Wl,-framework -Wl,CoreFoundation


JAVA_HOME        : /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags    : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Volumes/osx/201705_anaconda/anaconda/lib/R
Done.

which looks to be properly set-up as guided here and this SO thread unable to compile JNI program rJava so the JDK looks to be correctly installed.

I get the linker error with the command install.packages("rJava")

checking Java support in R... present:
interpreter : '/usr/bin/java'
archiver    : '/usr/bin/jar'
compiler    : '/usr/bin/javac'
header prep.: '/usr/bin/javah'
cpp flags   : '-I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/../include/darwin'
java libs   : '-L/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/lib/server -ljvm'
...
ld: library not found for -licuuc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libjri.jnilib] Error 1
make[1]: *** [src/JRI.jar] Error 2
make: *** [jri] Error 2
ERROR: compilation failed for package ‘rJava’

I have got the same error despite Java sources: official JDK and Homebrew's Java return the same linker/library error.

What could be causing this linker/library licuuc error with Anaconda and how to fix it?

Old threads

  1. Install rJava on macOS Sierra 10.12.1: linker error licuuc: a solution candidate with MacPorts

  2. rJava on MacOS Sierra 10.12.15: unsupported option fopenmp

  3. rJava load error in RStudio/R after "upgrading" to OSX Yosemite

  4. Install xlsx and rJava on macOS Mavericks 10.9.5

Fixes that I have tried

  1. options("java.home") returns NULL so options("java.home"="/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/") as guided by here

  2. Unistalled Homebrew's Java and replaced it with Oracele's Java and JDK here: the same licuuc library/linker error persists. I tried sudo Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")' guided by this but the same licuuc library not found error.

  3. Updated macOS from El Capitan 10.11.6 to Sierra 10.12.4 but the same licuuc library/linker error persists.

  4. Running without sudo, javac missing (following this here and this here). This error may be easiest to fix with pointing the javac directory there to /usr/bin/javac but better solution? When I run it with sudo, I got the same licuuc error.

    R CMD javareconf -e LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JAVA_LD_LIBRARY_PATH
    
    Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'
    
    Unable to locate an executable at "/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/javac"
    (-1)
        make[2]: *** [org/rosuda/JRI/Rengine.class] Error 2
        make[1]: *** [src/JRI.jar] Error 2
        make: *** [jri] Error 2
        ERROR: compilation failed for package ‘rJava’
    
  5. After removal of Anaconda conda install anaconda-clean; anaconda-clean --yes; rm -rf ~/anaconda, guided here, and installing R with cask such that brew cask install r-app; sudo R CMD javareconf; Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")' so getting a new error

    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’
    

What could be causing this error forenmp without Anaconda and how to fix it?

回答1:

I provide the solution in two ways: Anaconda and the Brew way. I suggest you to use the Brew solution. In both cases, I have Oracle's JDK installed and if you do this, remember reconfigure Java for R with sudo R CMD javareconf.

Anaconda (solution to the licuuc error)

A similar linker error occurred here. The anaconda path messes things up as

$ R CMD config --ldflags
  -L/Users/osx/anaconda3/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lm -liconv -licuuc -licui18n

so remove a path like below from ~/.bash_profile

export PATH="/Users/osx/anaconda3/bin:$PATH" #Removed to install rJava

so we should get something like

$ R CMD config --ldflags
  -F/Library/Frameworks/R.framework/.. -framework R -lpcre -llzma -lbz2 -lz -licucore -lm -liconv

and after this rJava can be installed even with Anaconda with

sudo R CMD javareconf 
Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'

congratulations!

Brew (solution to fopenmp error)

Your R installation should be done with Homebrew cask such that

brew cask install r-app

where brew's r package is not enough for this. Shortly, the forenmp problem is in the compiler not having the flag so we have to recompile the compiler. This solution is explained more thoroughly here. The problem in the question is for 3.3.* R and gcc solution used such that

#xcode-select --install #if Xcode commandline tools not installed
brew install homebrew/versions/gcc49 --without-multilib #Long ~70min compiling...
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

echo "VER=-4.9 
CC=gcc$(VER)
CXX=g++$(VER)
CXX1X=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/4.9.3/lib/gcc/4.9" > ~/.R/Makevars

and now

sudo R CMD javareconf 
Rscript -e 'install.packages("rJava", repos="http://rforge.net", type="source")'

and now rJava is working!

Solutions here mentioned also in the following because of similar problems

  1. Install rJava on macOS Sierra 10.12.1: linker error licuuc

  2. rJava on MacOS Sierra 10.12.15: unsupported option fopenmp