35

I'm new in C and have some problems compiling my code in OS X.

I code Java a lot both in Eclipse and use terminal to compile my code. However now I'm learning openMP and have troubles with it.

First I downloaded Xcode to write openMP code but it didn't recognize <omp.h>. Then I installed g++. When I type g++ -v into terminal I get this:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

But when I use g++ Mesh.cpp -fopenmp I still get

Mesh.cpp:4:10: fatal error: 'omp.h' file not found
#include <omp.h>
         ^
1 error generated.

Then I tried to install PTP into my Eclipse and got the same problem. I thought there was no omp.h in my MacBook so I searched for it and found several omp.h under folders under gcc-4.9.1/build/.

Here comes the problem. Based on the Java experience the only reason why I have the file but cannot use it is that the Class Path is wrong. However, I have no idea how to change this configuration in g++, or in Xcode, or in Eclipse. But since I can include files like <stdio.h> and compile it with all the IDEs, how can't I do the same with <omp.h>?

Another thing I noticed is that the gcc folder version is 4.9.1, but when I type gcc -v into terminal I get the same with typing in g++ -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix

Shouldn't the version information says something about 4.9.1? Just like what java -version shows

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

Thanks for reading. Any help is appreciated.

6 Answers 6

54

This command can help you

brew install libomp

brew info libomp
libomp: stable 6.0.1 (bottled)
LLVM's OpenMP runtime library
https://openmp.llvm.org/
/usr/local/Cellar/libomp/6.0.1 (12 files, 1.2MB) *
  Poured from bottle on 2018-11-20 at 16:12:22
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/libomp.rb
==> Dependencies
Build: cmake ✘
==> Requirements
Required: macOS >= 10.10 ✔
==> Caveats
On Apple Clang, you need to add several options to use OpenMP's front end
instead of the standard driver option. This usually looks like
  -Xpreprocessor -fopenmp -lomp

You might need to make sure the lib and include directories are discoverable
if /usr/local is not searched:

  -L/usr/local/opt/libomp/lib -I/usr/local/opt/libomp/include

For CMake, the following flags will cause the OpenMP::OpenMP_CXX target to
be set up correctly:
  -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include" -DOpenMP_CXX_LIB_NAMES="omp" -DOpenMP_omp_LIBRARY=/usr/local/opt/libomp/lib/libomp.dylib
3
  • 2
    Welcome. What does it do? How will it help? Please see How to Answer and take the Welcome tour. May 5, 2018 at 9:39
  • 8
    This is the actual solution for those of you using homebrew.
    – Gil Tomás
    Aug 13, 2018 at 10:24
  • I'm back from 9 years later. This question has 82k views so for those who use homebrew, this is the answer. Oct 9 at 14:21
18

MacOS seems to have that library included but XCode can't find it if you simply use:

#include <omp.h>

However, if you don't have the library installed,you can simply add it by installing it with HomeBrew:

brew install libomp

After doing this , simply replace library include code with this one:

#include "/usr/local/opt/libomp/include/omp.h"

or the path that terminal shows you after installing libomp with brew.

1
  • What if we want to make the code platform cross-compatible?
    – dcxst
    Jul 20 at 5:04
16

GCC 4.9.1 normally does not ship with OS X (actually no GCC ships with Xcode any more). Yours must have been installed by another means, e.g. Homebrew or self compilation as described here. What you are probably missing is properly set PATH variable or the additionally installed compiler has version-suffixed binaries, i.e. gcc-4.9 or g++-4.9 instead of simply gcc / g++.

As @rubenvb has already mentioned, Apple symlinks the Clang executables with GCC-like names. I personally find that a bad practice since recent Clang versions shipped with Xcode react on unrecognised command-line options (e.g. GCC frontend specific ones) with hard errors.

3
  • I installed GCC exactly followed the link you mentioned, but it still doesn't work... Sep 23, 2014 at 8:53
  • If you followed the instructions on the linked page, then your compiler is named g++-4.9 and you must explicitly add the path to it unless you installed into /usr/local/bin. Also, you have to reconfigure PTP to call g++-4.9 instead of g++. Sep 23, 2014 at 8:57
  • 1
    Thanks! After typing "export PATH=/usr/gcc-4.9.1/bin:$PATH", I can use "g++-4.9" now. Sep 23, 2014 at 9:08
13

The gcc and g++ commands are not what you think they are with XCode: Apple thought it would be a good idea to masquerade Clang as GCC to make the transition smoother.

Clang OpenMP support is still being worked on. If I didn't miss any big release of the WIP code, you'll need to build this version of clang and use that.

You can of course always install a real GCC through stuff like homebrew or macports, that will come with OpenMP support.

4
  • 3
    So basically the problem is GCC in my laptop is a fake by Clang? Since the latter one doesn't support openMP, it cause all the problem? Thank you. Sep 23, 2014 at 8:51
  • @user3928256 Yeah, the gcc command which you execute is provided by XCode, and is just Clang, really. I did miss the part of your question where you mention installing GCC 4.9.1. So, if you set up your PATH as described by @Hristo, you should be fine.
    – rubenvb
    Sep 23, 2014 at 9:24
  • Update from the linked website: Development activity of OpenMP support in clang/llvm compiler has moved to www.llvm.org. Please get OpenMP-enabled clang (OpenMP 3.1 is fully supported in clang/llvm 3.7) and contribute to its further development there. This web-site is maintained for archival purposes only. Mar 16, 2016 at 0:25
  • 2
    If you brew install gcc you can run the real gcc as gcc-6. Seriously Apple, that's some weird sh... Sep 14, 2016 at 11:56
6

The omp.h file has been moved to a subdirectory. I found it in MacPorts and solved this compilation problem by creating a link to this file:

sudo ln -s /opt/local/include/libomp/omp.h /opt/local/include/omp.h
1
  • 2
    MacPorts has a variant for this: sudo port install libomp +top_level Dec 3, 2019 at 21:32
2

if you are using M1 chip

brew install libomp here /opt/homebrew/Cellar/

so you have to link the libomp manually, after I installed brew install libomp, I got libomp 14.0.0

so use following command might help you

 g++ -Xpreprocessor -fopenmp  -I/opt/homebrew/Cellar/libomp/14.0.0/include -L/usr/local/lib -lomp  *.cpp -o main
1
  • 1
    I had to run ln -s /opt/homebrew/Cellar/libomp/14.0.5/include/omp.h omp.h from the project folder Jun 14, 2022 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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