Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

Starry picture Starry · Apr 22, 2017 · Viewed 29.8k times · Source

I am using Mac OS X Sierra, and I found that clang (LLVM version 8.1.0 (clang-802.0.38)) does not support OpenMP: when I run clang -fopenmp program_name.c, I got the following error:

clang: error: unsupported option '-fopenmp'

It seems that clang does not support -fopenmp flag.

I could not find any openmp library in homebrew. According to LLVM website, LLVM already supports OpenMP. But I could not find a way to enable it during compiling.

Does this mean that the default clang in Mac does not support OpenMP? Could you provide any suggestions?

(When I switch to GCC to compile the same program (gcc is installed using brew install gcc --without-multilib), and the compilation is successful.)

Answer

Dirk picture Dirk · Feb 12, 2018

Try using Homebrew's llvm:

brew install llvm

You then have all the llvm binaries in /usr/local/opt/llvm/bin. To compile the OpenMP Hello World program, for example, type

/usr/local/opt/llvm/bin/clang -fopenmp -L/usr/local/opt/llvm/lib omp_hello.c -o hello

You might also have to set the CPPFLAGS with -I/usr/local/opt/llvm/include.

A makefile should look like this:

CPP = /usr/local/opt/llvm/bin/clang
CPPFLAGS = -I/usr/local/opt/llvm/include -fopenmp
LDFLAGS = -L/usr/local/opt/llvm/lib

omp_hello: omp_hello.c
    $(CPP) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)

Update: In macOS 10.14 (Mojave) you might get an error like

/usr/local/Cellar/llvm/7.0.1/lib/clang/7.0.1/include/omp.h:118:13: fatal error: 'stdlib.h' file not found

If this happens, the macOS SDK headers are missing from /usr/include. They moved into the SDK itself with Xcode 10. Install the headers into /usr/include with

open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg