Compile Openmp Programs with Gcc Compiler on Os X Yosemite

Compile OpenMP programs with gcc compiler on OS X Yosemite

EDIT: As of 13 Aug 2017 the --without-multilib option is no longer present in Homebrew and should not be used. The standard installation

brew install gcc

will provide a gcc installation that can be used to compile OpenMP programs. As below it will be installed into /usr/local/bin as gcc-<version>. The current gcc version available from Homebrew (as of writing) will install as gcc-8. You can compile programs with OpenMP support using it via

gcc-8 -fopenmp hello.c

Alternatively you could put an alias in your .bashrcfile as

alias gcc='gcc-8'

and then compile using

gcc -fopenmp hello.c

Note: I'm leaving the original post here in case it is useful to somebody.

The standard gcc available on OS X through XCode and Clang doesn't support OpenMP. To install the Homebrew version of gcc with OpenMP support you need to install it with

brew install gcc --without-multilib

or as pointed out by @Mark Setchell

brew reinstall gcc --without-multilib

This will install it to the /usr/local/bin directory. Homebrew will install it as gcc-<version> so as not to clobber the gcc bundled with XCode.

Using OpenMP with C++11 on Mac OS

Updated Answer

Since my original answer below, the situation has improved and you can easily use OpenMP with the clang++ compiler - hurraaaay!

To do that, first use homebrew to install brew install libomp:

brew install libomp

Then when using clang++, use these flags:

clang++ -Xpreprocessor -fopenmp main.cpp -o main -lomp 

Original Answer

If you want to compile C++11 OpenMP code on OSX, the easiest way is to use gcc which you can install via homebrew.

First, check the available options:

brew options gcc

Sample Output

--with-all-languages
Enable all compilers and languages, except Ada
--with-java
Build the gcj compiler
--with-jit
Build the jit compiler
--with-nls
Build with native language support (localization)
--without-fortran
Build without the gfortran compiler
--without-multilib
Build without multilib support
--HEAD
Install HEAD version

So, I suspect you want:

brew install gcc --without-multilib --without-fortran

Once you have got it installed, you need to make sure you are using the homebrew version rather than the one Apple supplies. You need to know that homebrew installs everything in /usr/local/bin and that the C++ compiler is g++-6. So, you either need to compile with:

/usr/local/bin/g++-6 -std=c++11 -fopenmp main.cpp -o main

or, set up your PATH in your login profile:

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

then you can just do:

g++-6 -std=c++11 -fopenmp ...

Note that if you choose the second option above (i.e. the export PATH=... option), you will either need to also type the export command in your current session once to activate it, or log out and log back in since your profile commands are only executed on login.

AFAIK, there is no need to explicitly install libiomp - not sure why you did that.

GCC C/C++ MEX Matlab R2015 Mac OS X (with OpenMP) doesn't work

Finally, I found a proper way to solve it...
First, the file mexopts.sh doesn't appear in the folder by default, is necessary to open a terminal and look for it and create it (then Matlab will redirect to it automatically when compiling with MEX):

find ~/ -name 'mexopts.sh' 

and will appear:

/Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh

Then, copy it as:

cp /Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh ~/.matlab/R2014a

then go to the folder cd ~/.matlab/R2014a
and change permissions for your user as:

chmod u+rwx mexopts.sh

then, open it with your default text editor (Sublime text recommended) as:

open mexopts.sh

and edit the following:
Change where appears macosx10.7 to your current version, in my case macos10.10
then, modify the following lines as describen in (http://www.mathworks.com/matlabcentral/newsreader/view_thread/334250):

# CC='xcrun -sdk macosx10.7 clang' #OLD
CC='xcrun /usr/local/bin/gcc' #NEW

# CXX='xcrun -sdk macosx10.7 clang++' #OLD
CXX='xcrun /usr/local/bin/g++' #NEW

# CLIBS="$CLIBS -lstdc++" #OLD
CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> #NEW
#CXXLIBS="$MLIBS -lstdc++" #OLD
CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp" #NEW

IMPORTANT NOTE:
Make sure that your current G++/G++4.9 is able to compile with OpenMP, trying to include <omp.h> in a hello world file doing in the command line:

g++-4.9 -o test hello.cpp -fopenmp

or

g++ -o test hello.cpp -fopenmp

is also possible that a file is corrupted and is necessary to do Can not compile fortran because dyld: Library not loaded :

brew rm cloog

brew install cloog

(But check first)...

Is also possible that if you're not able to compile with OMP is necessary to do first a couple of things as described here (Compile OpenMP programs with gcc compiler on OS X Yosemite):
1. Got a new gcc complier from http://hpc.sourceforge.net/
2. Place a new executable folder by $ sudo tar -xvf gcc-4.9-bin.tar -C /
3. Switched to it by export PATH=/usr/local/bin:$PATH

Finally, try to compile your MEX file with:

mex hello.cpp COMPFLAGS="/openmp $COMPFLAGS"

That's it
... .

gcc-11 compiler optimisation levels not working on Mac m1

Optimization levels are specified with -O1 etc, using capital letter O, not lower-case letter o.

Lower-case -o1 specifies that the output file should be 1, and then out/jacobi2d1 is an input file to be linked, but it is an existing executable and you can't link one executable into another — hence the error from the linker.

How to include omp.h in OS X?

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.

OS X: installed gcc links to clang

brew installs tools in /usr/local/bin. Use /usr/local/bin/g++6:

$ /usr/local/bin/g++-6 --version
g++-6 (Homebrew gcc 6.2.0) 6.2.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Related Topics



Leave a reply



Submit