List

All screenshots are clickable.

For certain purposes, it is very beneficial to use OpenMP (shared memory parallelization) for the development.
This page covers the necessary steps to use OpenMP in Xcode (via Clang compiler).

The Clang compiler that is installed on Mac with Xcode does not have all the necessary features (at least at the moment of this post publication). I suggest installation of llvm compiler via Homebrew.

  1. Install Homebrew if it is not installed on your Mac machine.
  2. Install llvm by pasting the following command in your Terminal:
    brew install llvm

The following steps will be explained based on the “dummy project”; however, it should work for the existing project as well. The following code example is a standard one-thread HelloWorld.

#include <iostream>
int main(int argc, const char * argv[]) {
    std::cout << "Greetings from thread 1"<<std::endl;
    return 0;
}

  1. Specify a user-defined setting CC with the following value /usr/local/opt/llvm/bin/clang
    (If Homebrew changes the location of the llvm installation, the value of the user-defined setting CC has to be changed accordingly.)
    In order to do that:

    1. Select the project in the Project Explorer in the left and select Build Settings
    2. Select from the main Xcode menuEditor -> Add Build Setting -> Add User-Defined Setting
    3. Add the CC setting with the value /usr/local/opt/llvm/bin/clang
  2. Search for the setting Enable Modules (C and Objective-C) and switch it from Yes to No
  3. Add to the Library Search Paths the value /usr/local/opt/llvm/lib to allow for the precompiled libraries (for example, libiomp5) to be dynamically linked to your executable.
  4. Add to the Header Search Paths the value /usr/local/opt/llvm/lib/clang/4.0.1/include to allow inclusion of headers provided by llvm installation (for example, omp.h). Note, the installed version of Clang compiler might be different, so the value of the Header Search Path should be changed accordingly.
  5. Add the -fopenmp compilation flag to Other C Flags
  6. Link llvm OpenMP library dynamically to your executable. To do that:
    1. Select Build Phases -> Link Binary With Libraries options in the main Xcode window
    2. Open a Finder window and select Go To Folder option from the main menu.
    3. Navigate to /usr/local/opt/llvm/lib/ folder and select libiomp5.dylib file.
    4. Drag it to Xcode main window into the Add Frameworks and Libraries Here

Now the code should compile successfully, but already with llvm-provided Clang compiler.
Slightly editing the code to demonstrate that OpenMP is actually active:

#include <iostream>
#include "omp.h"
int main(int argc, const char * argv[]) {
#pragma omp parallel
#pragma omp critical
    std::cout << "Greetings from thread "<<omp_get_thread_num()<<std::endl;
    return 0;
}

Now the code greets us from all the threads (omp critical section is used so that the output is not “scrambled”)

The number of threads being used is usually controlled via environmental variable that can be edited via Terminal:

export OMP_NUM_THREADS=8

18 Responses to “Clang + OpenMP Setup in Xcode”

  1. Ramin

    Great article!
    I suffered for a whole day to get OMP work in my Xcode project but this article solved it in 10 minutes.
    Thank You!
    R.

  2. Laura

    When I try to compile the given code:

    #include
    #include “omp.h”
    int main(int argc, const char * argv[]) {
    #pragma omp parallel
    #pragma omp critical
    std::cout << "Greetings from thread "<<omp_get_thread_num()<<std::endl;
    return 0;
    }

    It gives me the error:

    Undefined symbols for architecture x86_64:
    "_omp_get_thread_num", referenced from:
    _main in main.o
    ld: symbol(s) not found for architecture x86_64
    clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

    Any suggestions? Thank you.

    • AntonMenshov

      It seems to me that the libiomp5.dylib was not linked to your executable. As it is not the compiler error, but the linker one. I suggest you check if the linker adds the libiomp5.dylib to your executable and if it does not, the article seems to address it in step 8.

  3. shihao

    Thanks a lot for your info. I’ve met an error by following your steps “Cannot specify -o when generating multiple output files”. Is there any suggestion? Thank you.

  4. Daniel

    Hello and thanks for your info … but, unfortunately I get the same error message:
    “Cannot specify -o when generating multiple output files”

    I have the following installation:
    llvm 5.0.1
    High Sierra 10.13.2
    Xcode Version 9.2

    • Daniel

      I am not an expert but maybe you could have a short look on the calling command and the error message:

      Command:
      CompileC /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/Objects-normal/x86_64/main.o threadTest/main.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
      cd /Users/daniel/src/threadTest/threadTest
      export LANG=en_US.US-ASCII
      /usr/local/opt/llvm/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu++14 -stdlib=libc++ -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror=return-type -Wdocumentation -Wunreachable-code -Werror=deprecated-objc-isa-usage -Werror=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.13 -g -fvisibility-inlines-hidden -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wunguarded-availability -index-store-path /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Index/DataStore -iquote /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/threadTest-generated-files.hmap -I/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/threadTest-own-target-headers.hmap -I/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/threadTest-all-target-headers.hmap -iquote /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/threadTest-project-headers.hmap -I/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Products/Debug/include -I/usr/local/opt/llvm/lib/clang/4.0.1/include -I/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/DerivedSources/x86_64 -I/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/DerivedSources -F/Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Products/Debug -fopenmp -MMD -MT dependencies -MF /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/Objects-normal/x86_64/main.d –serialize-diagnostics /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/Objects-normal/x86_64/main.dia -c /Users/daniel/src/threadTest/threadTest/threadTest/main.cpp -o /Users/daniel/Library/Developer/Xcode/DerivedData/threadTest-csrvzfdfenfcyyeqpmstfejxmhpl/Build/Intermediates.noindex/threadTest.build/Debug/threadTest.build/Objects-normal/x86_64/main.o

      Error Message:
      clang-5.0: error: cannot specify -o when generating multiple output files

    • Thiago

      This link solved problem.

      http://danlec.com/st4k#questions/46765974

  5. Anthony

    I went through these steps and xcode is giving me an error of omp.h file not found. Any help would be much appreciated.

    • AntonMenshov

      Hi Anthony,
      Can you please make sure that you have done Step 6?
      omp.h is supposed to be in /usr/local/opt/llvm/lib/clang/4.0.1/include
      (maybe you have a different version of clang, then the path will slightly change).

      Make sure the file is there and you have done Step 6 of the instruction.

  6. Arturo

    There is an error in step 8c. The name of the file is not libimpl5.dylib; it is libiomp5.dylib, as the picture shows. Other than that, it worked perfectly! Thanks a lot, Mr. Menshov.

    • AntonMenshov

      Thanks a lot for pointing out the mistake! It is fixed now.

  7. ATevet

    Thanks for this article!

  8. Jacob

    Thanks for putting this together. For some reason when I try to compile the unedited code after step 8. I get an error saying :
    error: can’t exec ‘ /usr/local/opt/llvm/bin/clang’ (No such file or directory)
    Command /usr/local/opt/llvm/bin/clang failed with exit code 71

    Could this have something to do with me running OSX 10.10.5

    Thanks for the help.

    • AntonMenshov

      A bit strange. Can you try to run the following command in your terminal:

      xcode-select –install

      This might help.

  9. Green Moon

    how will this work for an automated build? I am using VSTS to do the iOS build but it obviously doesn’t like /usr/local/ path.

    • AntonMenshov

      Sorry to say, have no idea and no experience using automated build and continuous integration in Xcode.

Leave a Reply

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