OpenMP Clang in Xcode

I’ve been playing around with prime numbers recently, which (naturally?) extended into an investigation into parallel programming. I installed the latest version of GCC 5 (v5.3.0 at the time of writing) using these instructions and got OpenMP running (support is built-in). I’m fine with using Terminal but I thought it would be interesting to try this in Xcode.

I installed the OpenMP C/C++ language extensions for Clang using homebrew with instructions from here. Unfortunately, I received the following error:

can't exec '/usr/local/bin/clang++-omp' (No such file or directory)

It turns out those instructions were missing a symlink that needs to be created. It’s as follows:

sudo ln -s /usr/local/bin/clang-omp++ /usr/local/bin/clang++-omp

Thanks to Stack Overflow for the “full” instructions. I can now build and run C++ parallelized code directly inside Xcode. I am finding a performance difference running the same code via the Terminal vs inside Xcode (which is slower). Hmm…

6 thoughts on “OpenMP Clang in Xcode

  1. John Nairn

    Another trick to compile OpenMP code in XCode is to install a plug in use GCC as the compiler. The steps I used in some scientific code are given here:

    http://osupdocs.forestry.oregonstate.edu/index.php/Compiling_in_MacOS_X#MacOS_X_Compiler

    Update: The above process works great up to XCode 7, but I recently “upgraded” to MacOS Sierra and XCode 8. While one can still compile using GCC, the linking step fails. The problem is an compiler option trying to link to Sierra SDK (which I did not need for a c tool). Although similar options caused no problems prior to XCode 8, the link now says a library has the wrong format. I am still looking for a work around. I did find that MS Visual Studio Community 2015 (free) can compile my code and OpenMP is an supported option (along with other features, like Java, that are slowly being removed from XCode).

    Note: If you use an XCode plug in for GCC, be sure to save a copy because each update of XCode will delete your installed plug in.

    Reply
    1. Norm Post author

      Given the status of plugins in Xcode 8, I don’t see this (your solution) as viable moving forward. If you look at the Stack Overflow link I posted above, there are additional comments on how to get this working (via MacPorts) with Xcode 8 (posted Dec 2016).

      Using OpenMP in Xcode was always just a curiosity for me, if I need serious (parallel) performance, I’ll write the code in C/C++ and just run it from the command line.

      Reply
  2. Mono Wang

    I had trouble for step 3 and step 4:
    3. Add a new user-defined setting CC with the value /usr/local/bin/clang-omp under the project’s build settings
    4. Add -fopenmp to Other C Flags under the project’s build settings.

    After adding “-fopenmp to Other C Flags”, I got error “Unsupported option ‘-fopenmp'” in xcode 8.
    But, in terminal, I can successfully /usr/local/bin/clang-omp++ -fopenmp.

    BTW, I don’t quite understand “Add a new user-defined setting CC” in step 4. In xcode, I can only find a “+” button to add a new “user-defined setting” in the project’s build settings, but not the “user-defined setting CC”. Not sure if they are the same thing.

    It would be highly appreciated if you could kindly help me out of the mess. Thanks.

    Reply
  3. Norm Post author

    Hi Mono,

    If you are using Xcode 8, this process doesn’t work. You need to open the Stack Overflow link I posted above and look down to the last comment of the approved answer. It describes how to install clang-3.8 via MacPorts, which appears to be the new way to get OpenMP support in Xcode.

    As far as the Build Settings, you need to select your target, then select Build Settings, and scroll down to the compiler area (Apple LLVM 8.0 – Custom Compiler Flags) and enter the info in there.

    I hope this helps,
    Norm

    Reply
    1. Mono

      Hello Norm,

      Thanks for the reply and details. In the Stack Overflow post that shows how to install clang-3.8 via MacPorts, I found in the step “[2] User-defined setting CC /opt/local/bin/clang-mp/3.8”, I assume it is a typo that supposes to be “/opt/local/bin/clang-mp-3.8”.

      Regarding the “User-defined setting CC”, I am still confused. I don’t know where I should input the value “/opt/local/bin/clang-mp-3.8” (see the attached figure, “the macOS Default” is not editable).

      Many thanks for your patience,
      Mono

      Reply
      1. Norm Post author

        Hi Mono,

        Sorry, I haven’t looked at this for a while. I think this will help. Visit the following link , scroll down to the “Using clang-omp with Xcode” section. It describes how to enter the CC information. You need to create a new, user-defined setting. Here’s what it looks like in one of my Xcode projects: Xcode user-defined CC settings

        For what it’s worth, this broke for me under macOS Sierra (10.12.3 currently). I can still run this from Terminal but my Xcode project doesn’t work. I attempted a (quick?) rebuild using Homebrew following the instructions at the link above and it crashed under Sierra. Note: It does say that Sierra is not supported.

        Norm

        Reply

Leave a Reply to Mono Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.