52

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.)

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
Starry
  • 673
  • 2
  • 7
  • 14

4 Answers4

39
  1. Try using Homebrew's llvm:

    brew install llvm
    
  2. 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
blkpingu
  • 1,556
  • 1
  • 18
  • 41
Dirk
  • 2,335
  • 24
  • 36
  • 8
    You may also need `brew install libomp` – Increasingly Idiotic Apr 16 '18 at 21:04
  • 1
    Indeed, with `llvm@8`, I had to install `libomp` – Ryan H. Jun 15 '19 at 18:33
  • This 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 by `omp_hello'. Stop.` Damned if I do, damned if I don't. – rocksNwaves Feb 02 '21 at 17:01
  • @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. – Ed. Apr 19 '21 at 16:14
  • 1
    @Ed. Thanks for noting, I included the file in my answer. – Dirk Apr 20 '21 at 10:16
  • 1
    This 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 do `brew install llvm`, it will let you know if you need to do this. – Rob May 30 '21 at 19:32
  • 2
    For me the compile command was `/opt/homebrew/opt/llvm/bin/clang -fopenmp -L/opt/homebrew/opt/llvm/lib omp_hello.c ` – Fab von Bellingshausen Jul 20 '22 at 20:12
32

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.

Yongwei Wu
  • 5,292
  • 37
  • 49
  • I tried that, but it wouldn't find `omp.h` for inclusion. – Walter Jun 10 '20 at 11:51
  • If you are sure you have install libomp under Homebrew, you can ask on the Homebrew forum https://discourse.brew.sh/. In my case, omp.h is installed under /usr/local/Cellar/libomp/10.0.0/include, symlinked to /usr/local/include. – Yongwei Wu Jun 12 '20 at 15:07
  • 15
    Is it known why Apple clang requires the `-Xpreprocessor` flag? – user76284 Jun 17 '20 at 06:52
15

MacOS Mojave with CMake

  1. Install LLVM with openmp and libomp with brew

     brew update
     brew install llvm libomp
    
  2. 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")
    
  3. 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

Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101
  • After 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. – Jacob Jul 10 '19 at 17:23
  • 2
    Where is the `CMakelists.txt` ? I have at least 6 files named this way. – Py-ser Jul 16 '19 at 21:50
  • Thank you so much! It works for me in Clion and Mac OS 10.14.6 – SBlincov Sep 20 '19 at 19:17
  • 1
    An improvement on this answer would be to use `brew --prefix llvm` rather than assuming /usr/local/opt/llvm. Something like `execute_process(COMMAND brew --prefix llvm OUTPUT_VARIABLE LLVM_PREFIX)` – rgov Mar 06 '20 at 17:58
  • You can add `set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang") set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang++")` in `CMakeList.txt`, so you can run cmake without -D – Dmitry Grebenyuk Aug 24 '20 at 02:54
  • This is a more modern solution https://stackoverflow.com/a/51448364/7604852 – Dmitry Grebenyuk Aug 24 '20 at 12:40
0

Conda-Based Compilation Environment

Conda uses clang for OSX compilation (umbrella package cxx-compiler), but I hit similar issues with using llvm-openmp and the -fopenmp flag throwing errors. Solution is rather similar to other answers, but I am including here in case others have more exactly this issue.

Specific solution was to include the Conda environment's include/ directory in the CFLAGS, namely:

CFLAGS="-I${CONDA_PREFIX}/include"

Note, I also needed to add -lstdc++ -Wl,-rpath ${CONDA_PREFIX}/lib -L${CONDA_PREFIX}/lib when linking, similar to this GitHub Issue.

merv
  • 67,214
  • 13
  • 180
  • 245