Using Openmp with C++11 on MAC Os

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.

Using x86 libraries and OpenMP on macOS arm64 architecture

Using an x86 installation of brew solves the problem for me. Here is a minimal set of commands for installing x86 variants of brew and clang, and then compiling my C/C++ code:

# launch x86_64 shell
arch -x86_64 zsh
# install x86_64 variant of brew
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
# install x86_64 variant of clang
arch -x86_64 /usr/local/bin/brew install llvm
# compile using x86_64 variant of clang
/usr/local/opt/llvm/bin/clang++ -arch x86_64 omp_ex.cpp

The brew application is now installed in two separate locations on my machine:

# arm64 (default) location
/opt/homebrew/bin/brew
# x86_64 location
/usr/local/bin/brew

and clang is installed in three separate locations:

# Apple arm64 (default) location
/usr/bin/clang
# brew arm64 location
/opt/homebrew/opt/llvm/bin/clang
# brew x86_64 location
/usr/local/opt/llvm/bin/clang

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.

OpenMP on macOS Mojave

OpenMP code can be compiled with AppleClang on mojave. Xcode includes an openmp-aware preprocessor which must be invoked.

  • You must have an omp installed; libomp is the most common. I have also used libiomp5.

  • Adjust the flags in cmake to point to your openmp solution.:

cmake .. -DCMAKE_C_COMPILER="clang" -DCMAKE_CXX_COMPILER="clang++" -DOpenMP_C_FLAGS=-fopenmp=lomp -DOpenMP_CXX_FLAGS=-fopenmp=lomp -DOpenMP_C_LIB_NAMES="libomp" -DOpenMP_CXX_LIB_NAMES="libomp" -DOpenMP_libiomp5_LIBRARY="/opt/local/lib/libomp.dylib" -DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp /opt/local/lib/libomp.dylib -I/opt/local/include" -DOpenMP_CXX_LIB_NAMES="libomp" -DOpenMP_omp_LIBRARY=/opt/local/lib/libomp.dylib -DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp /opt/local/lib/libomp.dylib -I/opt/local/include" 

Similar flags for libiomp5: See this line.

Compiling and linking against OpenMP with AppleClang on Mac OS X Mojave

As commented by Tsyvarev, the solution is to use the "updated" way of including OpenMP in CMake:

cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)

set(CMAKE_CXX_STANDARD 17)

add_executable(OpenMPTest main.cpp)

find_package(OpenMP REQUIRED) # Find the package
target_link_libraries(${PROJECT_NAME} ${OpenMP_CXX_LIBRARIES}) # Link against it for C++

This compiled on Windows, Ubuntu and Mac OS X using their platform's default compilers respectively.

The accepted answer here is not recommended even though it also has the most upvotes.



Related Topics



Leave a reply



Submit