36

How can I get OpenMP to run on Mac OSX 10.11, so that I can execute scripts via terminal?

I have installed OpenMP: brew install clang-omp.

When I run, for example: gcc -fopenmp -o Parallel.b Parallel.c the following expression returns: fatal error: 'omp.h' file not found

I have also tried: brew install gcc --without-multilib but unfortunately this eventually returned the following (after first installing some dependencies):

The requested URL returned error: 404 Not Found
Error: Failed to download resource "mpfr--patch"

Any recommended work arounds?

5

7 Answers 7

45

On a Mac, the command gcc is a symlink to Clang. So by calling

gcc -fopenmp -o your_program your_program.c

you are in fact using Clang, which until now has not had built-in support for OpenMP.

The newer versions of Clang do have support for OpenMP according to this post (where you can also find instructions on how to set it up).

On the other hand, if you still want to use gcc I can guide you through the steps that worked for me.

  1. Install gcc with brew. The command you used should work:

    brew install gcc --without-multilib
    

    Alternatively, if brew says that you already have gcc installed you can try

    brew reinstall gcc --without-multilib
    

    As you may have noted, if you don't specify --without-multilib brew warns you that OpenMP may not work.

  2. Find the location of the newly installed gcc.

    Brew appends the version number to gcc so that it does not conflict with the one installed by Command Line Tools. You will find the symlink in usr/local/bin.

    In my case it's

    usr/local/bin/gcc-5
    

    If you right-click and chose "Show original" it should show the gcc-5 executable in /usr/local/Cellar/gcc/5.3.0/bin/gcc-5 (version numbers may differ).

  3. Now you need to tell your system about it:

    When calling a compiler your bash will look into /usr/bin by default and not in /usr/local/bin. You need to add this directory to your $PATH.

    This can be easily done with the command:

    PATH=/usr/local/bin:$PATH
    
  4. Now you should be able to compile with OpenMP enabled using:

    gcc-5 -fopenmp -o your_program your_program.c
    

Remark: gcc-5 is the version I have installed, yours might differ.

9
  • Works for me, with the current version being gcc-6. Although if you're installing homebrew for the first time, shouldn't the $PATH modification be written into your .bashrc?
    – APaul
    Commented Jan 31, 2017 at 18:04
  • 1
    Where are the files? I get an error saying there is no input file
    – SumNeuron
    Commented Sep 17, 2017 at 8:24
  • Which files are you refering to @SumNeuron? If you mean the ones in the last code line I gave the first "Parallel" is the name of the output executable and "Parallel.c" is the source to compile. Your terminal must be in the same directory as the "Parallel.c" file, else it won't find it. Commented Sep 18, 2017 at 5:39
  • @AlejandroDanielNoel my question is where do you get those files as the do not seem to be installed with gcc or llvm
    – SumNeuron
    Commented Sep 18, 2017 at 7:45
  • 3
    Just tried these steps and received the following notification from Homebrew: Warning: gcc: this formula has no --without-multilib option so it will be ignored! Commented Apr 3, 2018 at 19:18
16
  • install clang-omp

     brew install clang-omp
    
  • make sure you have installed the xcode command line tool

     xcode-select --install
    
  • I actually had one error while running a sample openmp code

     /usr/local/opt/libiomp/include/libiomp/omp.h:139:21: error: expected ';' after top level declarator extern void   __ KAI_KMPC_CONVENTION kmp_set_stacksize_s        (size_t);
    
  • Just remove one space that is present between __ and KAI from the file

  • Now use the command

     clang-omp -fopenmp helloopenmp.c
    

and run the following code

    #include <omp.h>
    #include <stdio.h>
    int main() {
        #pragma omp parallel
        printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
    }
  • You should get output similar to this

     Hello from thread 3, nthreads 4
     Hello from thread 2, nthreads 4
     Hello from thread 0, nthreads 4
     Hello from thread 1, nthreads 4
    
  • Worked on OS X 10.11.3 and with brew update dated 18th Feb 2016

1
  • 15
    clang-omp was deleted from homebrew/core in commit c57e307: clang-omp: migrate to boneyard Functionality is now available as part of LLVM in Homebrew.
    – saladi
    Commented Apr 17, 2017 at 21:31
8

macOS High Sierra Version 10.13.6 (17G65)

1. Install gcc

brew reinstall gcc --without-multilib

2. Compile

gcc-8 -Wall -fopenmp your-parallel-program.c

Notice the gcc-8 watch the version brew installs, yours may be gcc-7 or gcc-9

==> Pouring gcc-8.2.0.high_sierra.bottle.1.tar.gz 🍺 /usr/local/Cellar/gcc/8.2.0: 1,495 files, 344.8MB

That's it!

2
  • 10
    No need to add --without-multilib any more in 2019.
    – Louis Yang
    Commented Jan 7, 2019 at 10:07
  • 1
    invalid option: --without-multilib
    – Skyy2010
    Commented Mar 13, 2019 at 14:18
8

By default, GCC is using clang distribution. Install GCC with Homebrew. Once installed, you can compile the code specifying the GCC version installed by Homebrew and adding the OpenMP flag. A hello_world example is shown below:

  1. brew install gcc

  2. brew install libomp

  3. brew info gcc

    gcc: stable 10.2.0 (bottled), HEAD

    GNU compiler collection

  4. export OMP_NUM_THREADS=8

  5. create hello.c

#include <stdio.h>
#include <omp.h>
int main(void){
    #pragma omp parallel 
    {
        printf("Hello World! \n");
    }

    return 0;
}
  1. gcc-10 -fopenmp hello.c -o hello
  2. Execute: ./hello
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
1
  • this does not add any value to existing and upvoted answers. Commented Dec 21, 2020 at 4:11
2
  1. Install gcc using brew.

    brew install gcc
    
  2. Check gcc is installed.

    $which g++-7
    /usr/local/bin/g++-7
    
  3. Change cmake cxx compiler.

    cmake -DCMAKE_CXX_COMPILER=g++-7
    make
    

Maybe this will help you.

0

After install gcc:

brew install gcc --without-multilib

and export PATH:

export PATH=/usr/local/bin:$PATH

You may need to export CC, which works for me:

export CC=/usr/local/bin/gcc

This maybe gcc-7, or whatever.

0
brew install cmake
brew install gcc --without-multilib
cmake -DCMAKE_CXX_COMPILER=g++-6 .. 
make -j 

Not the answer you're looking for? Browse other questions tagged or ask your own question.