For those impatient, skip to how to enable OpenMP in packages.
Apple has explicitly disabled OpenMP support in compilers that they ship in Xcode:
$ clang -c omp.c -fopenmp clang: error: unsupported option '-fopenmp'even though clang had OpenMP support for quite a long time now (great thanks to the folks at Intel providing their library as open source!). In fact, the clang compiler in Xcode can generate all the necessary code for OpenMP. It can be tricked into performing its designed function by using -Xclang -fopenmp flags.
The unfortunate part about this is that Apple is not shipping the necesssary libomp.dylib run-time library needed for OpenMP support. To make things worse, the version of the library you need depends on the clang version used, which Apple obfuscates so that it's non-trivial to reverse-engineer it. Fortunately, some clever folks were able to find the traces in Apple's released source so we can build the binaries that correspond to the clang version used. It is sometimes possible to use a more recent version of the runtime than the version of Apple clang.
curl -O https://mac.r-project.org/openmp/openmp-12.0.1-darwin20-Release.tar.gz sudo tar fvxz openmp-12.0.1-darwin20-Release.tar.gz -C /The contained set of files is the same in all tar balls:
usr/local/lib/libomp.dylib usr/local/include/ompt.h usr/local/include/omp.h usr/local/include/omp-tools.hso you can simply remove those to uninstall. Note that any package you compile against libomp.dylib will need that run-time so you have to ship it with your package or have users install it. You can verify the signature in the library via codetool (see below).
(built on macOS 12, but works on macOS 11)
Xcode 13+ (Apple clang 1300.x)
|LLVM 11.0.1 (+M1 patch)
Xcode 12.5 (Apple clang 1205.x)
Xcode 12.0-12.4 (Apple clang 1200.x)
Xcode 11.4-11.7 (Apple clang 1103.x)
Xcode 11.0-11.3.1 (Apple clang 1100.x)
Xcode 10.2-10.3 (Apple clang 1001.x)
PKG_CPPFLAGS='-Xclang -fopenmp' PKG_LIBS=-lomp R CMD INSTALL myPackageIf that doesn't work, please consult the package's documentation, and liaise with its maintainer. It is also possible to add those flags globally by adding the following to ~/.R/Makevars:
CPPFLAGS += -Xclang -fopenmp LDFLAGS += -lompbut be very careful when doing this, always check your ~/.R/Makevars whenever you upgrade R, macOS or Xcode.
$ codesign -d -vv /usr/local/lib/libomp.dylib Executable=/usr/local/lib/libomp.dylib Identifier=libomp Format=Mach-O universal (x86_64 arm64) CodeDirectory v=20400 size=5514 flags=0x0(none) hashes=167+2 location=embedded Signature size=8927 Authority=Developer ID Application: Simon Urbanek (VZLD955F6P) Authority=Developer ID Certification Authority Authority=Apple Root CA Timestamp=11/11/2021 at 1:29:04 PM Info.plist=not bound TeamIdentifier=VZLD955F6P Sealed Resources=none Internal requirements count=1 size=168
Last modified on 2021/11/11 by Simon Urbanek