5

I am trying to install a package on R called XBART. I use RStudio as my IDE, and am installing in the console: 'install_github("jingyuhe/XBART")'. The package requires a C++ compiler, and I am using clang++. However, I keep getting the following error:

clang: error: unsupported option '-fopenmp'

From searching around, it seems that the Apple clang doesn't support OpenMP, which is why I am getting this error. I've tried to install an outside clang ('brew install llvm') and OpenMP ('brew install libomp'). It keeps using the Apple version of clang for the download, though. How do I make R use this outside compiler for the download instead of the Apple version?

Botje
  • 26,269
  • 3
  • 31
  • 41
Oliver R
  • 65
  • 1
  • 1
  • 5
  • You need the `-Xpreprocessor` flag with `-fooenmp` when using `Apple clang`. – Richard Barber Oct 07 '20 at 08:14
  • 1
    It gets more complicated with MacOS Big Sur, see: https://stackoverflow.com/a/65334247/12957340 – jared_mamrot Dec 18 '20 at 02:01
  • @jared_mamrot: I'm learning C++ and the tutorial told me that I can install homebrew version of `clang`. My question is that what are the situations that we don't use the builin (xcode) version `clang`(or `clang++`)? – NeoZoom.lua Dec 19 '21 at 16:52
  • 1
    In my experience, gcc/gcc++ installed via homebrew is compatible with openMP. If your code uses openMP for parallelization (i.e. you need to compile it with the -fopenmp flag), you want to use the homebrew version because the macOS built-in clang/clang++ isn't able to compile the source code properly. There are other reasons to favour gcc or clang, but this was the main reason for me @Rainning – jared_mamrot Dec 19 '21 at 22:28
  • @jared_mamrot: Really really thank you! – NeoZoom.lua Dec 21 '21 at 14:50

2 Answers2

2

You could install brew llvm/clang with:

brew update
brew install llvm

The highest clang version in brew is 10.0.1 at the moment of writing.

This will normally install llvm under /usr/local/opt/llvm for not to colide with the apple clang.

You can either set /usr/local/opt/llvm/bin in front of PATH or set CC and CXX environment variables appropriatelly.

On my machine I have compiled the newest LLVM from sources (version 12), installed it under /usr/local/opt/myllvm and set CC/CXX to clang/clang++.

StPiere
  • 4,113
  • 15
  • 24
0

Install your desired version:

brew install llvm@14

Then insert the following in ~/.zshrc (change versions if different):

PATH="/opt/homebrew/Cellar/llvm@14/14.0.6/bin${PATH:+:${PATH}}"; export PATH;

Upon opening a new shell, check the version:

clang --version
Homebrew clang version 14.0.6
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /opt/homebrew/Cellar/llvm@14/14.0.6/bin