Clang + OpenMP Setup in macOS
The built-in Clang compiler in Xcode does not have all the necessary features. Since November 27, 2015 OpenMP support has been included in clang/llvm compiler, therefore we’ll use llvm instead.
The llvm installation is pretty simple: just enter
brew install llvm in the terminal window. Now you have two
clang/clang++ compilers: system and llvm.
To make llvm compiler by default edit the global variable
$PATH, in my case (I use fish shell) I edited
~/.config/fish/config.fish to set
$PATH and a variable for compilation flags as below:
Eventually, to compile your program with included OpenMP you should enter something like this in the terminal:
And the following steps will describe configuration of Xcode base on the standard one-thread HelloWorld. However, it should work for the existing project as well.
1. Specify a user-defined setting CC with the following value
/usr/local/opt/llvm/bin/clang. To do that
- click on the project in Project Navigator, then go to Build settings
- on the top menu bar got to Editor →️ Add Build Setting → Add User-Defined Setting and add new CC setting with value
2. In the same place search for the setting Enable Modules and switch it from Yes to No:
3. Now search for the Search path and add
/usr/local/opt/llvm/lib to the Library Search Paths and
/usr/local/opt/llvm/lib/clang/5.0.0/include/ to the Header Search Paths. Note, that the version of clang in the path above can differ from this case, so change it if it is needed.
4. Search for the Other C Flags and add the -fopenmp compilation flag to it:
5. This step is optional. You can add precompiled
libomp to your project. Go to Build Phases → Link Binary With Libraries, click on the add button, then Add Other…, press ⌘+⇧+G and enter
/usr/local/opt/llvm/lib, scroll to the bottom, find
libomp.dylib (may differ a little) and click twice on it.
Finally, run the project and look at the output. You can specify the number of threads with
Thanks for reading and good luck! 🙂