Enable OpenMP support in clang in Mac OS X (sierra & Mojave)
Solution 1
-
Try using Homebrew's llvm:
brew install llvm
-
You then have all the llvm binaries in
/usr/local/opt/llvm/bin
.Compile the OpenMP Hello World program. Put
omp_hello.c
/****************************************************************************** * FILE: omp_hello.c * DESCRIPTION: * OpenMP Example - Hello World - C/C++ Version * In this simple example, the master thread forks a parallel region. * All threads in the team obtain their unique thread number and print it. * The master thread only prints the total number of threads. Two OpenMP * library routines are used to obtain the number of threads and each * thread's number. * AUTHOR: Blaise Barney 5/99 * LAST REVISED: 04/06/05 ******************************************************************************/ #include <omp.h> #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { int nthreads, tid; /* Fork a team of threads giving them their own copies of variables */ #pragma omp parallel private(nthreads, tid) { /* Obtain thread number */ tid = omp_get_thread_num(); printf("Hello World from thread = %d\n", tid); /* Only master thread does this */ if (tid == 0) { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); } } /* All threads join master thread and disband */ }
in a file and use:
/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
.The 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
Solution 2
Other people have given one solution (using Homebrew llvm). You can also use OpenMP with Apple Clang and Homebrew libomp (brew install libomp
). Just replace a command like clang -fopenmp test.c
with clang -Xpreprocessor -fopenmp test.c -lomp
.
Solution 3
MacOS Mojave with CMake
-
Install LLVM with openmp and libomp with brew
brew update brew install llvm libomp
-
add include directories and link directories in
CMakeList.txt
include_directories("/usr/local/include" "/usr/local/opt/llvm/include") link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
-
run CMake with the new compilers
cmake -DCMAKE_C_COMPILER="/usr/local/opt/llvm/bin/clang" -DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++" ..
The clang version is 7.0.1 at time of writing
Related videos on Youtube
Starry
Updated on September 16, 2021Comments
-
Starry over 2 years
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.)-
Anton Korobeynikov about 7 yearsIndeed, Apple-provided clang does not support OpenMP.
-
cbrnr almost 7 years
brew install llvm
should install the latest LLVM version, i.e. 4.0.0. Does this fix the problem? -
Increasingly Idiotic about 6 yearsApple-provided clang does not support OpenMP by default. It is possible to enable the feature in Apple-provided clang and also possible to install a more recent version of clang that does support OpenMP by default.
-
MarcusJ about 5 years@IncreasinglyIdiotic How do we enable it? do we just need to compile and install the openmp runtime?
-
Increasingly Idiotic about 5 years@MarcusJ you should just need to
brew install llvm libomp
and then make sure to use the new clang to compile with the-fopenmp
flag
-
-
Increasingly Idiotic about 6 yearsYou may also need
brew install libomp
-
Ryan H. almost 5 yearsIndeed, with
llvm@8
, I had to installlibomp
-
Jacob almost 5 yearsAfter trying to build Blender on macOS (which has libomp as a dependency) for 3 days, this is the solution that worked for me, though my symptoms were a bit different. For me the compilation completed, but there were linker errors about missing x86_64 symbols from the libomp library. This worked for Homebrew's GCC 9.1.0 as of this comment.
-
Py-ser almost 5 yearsWhere is the
CMakelists.txt
? I have at least 6 files named this way. -
SBlincov over 4 yearsThank you so much! It works for me in Clion and Mac OS 10.14.6
-
rgov about 4 yearsAn improvement on this answer would be to use
brew --prefix llvm
rather than assuming /usr/local/opt/llvm. Something likeexecute_process(COMMAND brew --prefix llvm OUTPUT_VARIABLE LLVM_PREFIX)
-
Walter almost 4 yearsI tried that, but it wouldn't find
omp.h
for inclusion. -
Yongwei Wu almost 4 yearsIf you are sure you have install libomp under Homebrew, you can ask on the Homebrew forum discourse.brew.sh. In my case, omp.h is installed under /usr/local/Cellar/libomp/10.0.0/include, symlinked to /usr/local/include.
-
user76284 almost 4 yearsIs it known why Apple clang requires the
-Xpreprocessor
flag? -
Dmitry Grebenyuk over 3 yearsYou can add
set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang") set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")
inCMakeList.txt
, so you can run cmake without -D -
Dmitry Grebenyuk over 3 yearsThis is a more modern solution stackoverflow.com/a/51448364/7604852
-
rocksNwaves about 3 yearsThis resulted in a "missing separator" error for me. Apparently, that means tabs have to be "hard". So I found the "soft" tab and replaced it, and then I got the error:
*** No rule to make target
omp_hello.c', needed byomp_hello'. Stop.
Damned if I do, damned if I don't. -
Ed. about 3 years@rocksNwaves The instructions given here are how to compile a file called
omp_hello.c
. Make is telling you that file doesn't exist and it doesn't know what to do. Solution: provide that file. -
Dirk about 3 years@Ed. Thanks for noting, I included the file in my answer.
-
Rob almost 3 yearsThis worked for me (macOS 11.4). For future readers, if you that have Xcode installed, you might need to do
xcode-select --install
to install the command line utilities. When you dobrew install llvm
, it will let you know if you need to do this.