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

29 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.

    • Aaron

      I just had the same issue, and it was due to a stray space that I had copied in front of /usr. Just delete that and you’re golden!

      • AntonMenshov

        That should be solving Jacob’s problem. I did not notice that there is a space before /usr inside of the quotation marks!

  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.

  10. Aaron

    Is it possible to use this when building for iOS? When I follow the instructions, it says

    ld: warning: URGENT: building for iOS simulator, but linking against dylib (/usr/local/Cellar/llvm/6.0.0/lib/libomp.dylib) built for OSX. Note: This will be an error in the future.

    That dylib is built only for 64-bit macOS. Where would I be able to find OpenMP library builds for iOS and the simulator?

    The build log indicates that compiling sources uses the HomeBrew-installed clang binary, but when linking, it uses /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++

    • AntonMenshov

      Hmm. I have zero knowledge about how/if OpenMP works on iOS. But you certainly cannot link your executable to libomp.dylib for Mac.

  11. Charly

    I’ve followed every step but I’m still getting the same error :
    Apple LLVM 9.0 error
    > Unsupported option ‘-fopenmp’

    Does anyone ever encoutered this error of anyone would have any clues on how to build my project ?
    Thanks.

    If this could help any way :

    CompileC /Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/Objects-normal/x86_64/maxflow.o GraphCutsPerso/maxflow.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
    cd /Users/CharlyGirot/Documents/CPE/GraphCuts/Sources
    export LANG=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -Wno-trigraphs -fpascal-strings -O3 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-float-conversion -Wno-non-literal-null-conversion -Wno-objc-literal-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\”Release\” -DCORRECTION -DNDEBUG -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 -Wno-sign-conversion -Wno-infinite-recursion -Wno-move -Wno-comma -Wno-block-capture-autoreleasing -Wno-strict-prototypes -Wno-range-loop-analysis -I/Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/Release/include -I/Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso -isystem /usr/local/include -isystem /usr/local/include/opencv -I/Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/DerivedSources/x86_64 -I/Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/DerivedSources -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -F/Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/Release -DNDEBUG -Wall -Wextra -std=c++11 -Wno-comment -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -fopenmp -MMD -MT dependencies -MF /Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/Objects-normal/x86_64/maxflow.d –serialize-diagnostics /Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/Objects-normal/x86_64/maxflow.dia -c /Users/CharlyGirot/Documents/CPE/GraphCuts/Sources/GraphCutsPerso/maxflow.cpp -o /Users/CharlyGirot/Documents/CPE/GraphCuts/build/GraphCutsPerso/GraphCuts.build/Release/graph_cuts_perso.build/Objects-normal/x86_64/maxflow.o

    • AntonMenshov

      /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang tells me that you are still using the built-in XCode clang instead of a custom one that is suggested to install in the post.

  12. Alex

    I did everything as you said, the omp.h error is not there anymore, but I’m having this problem now:
    clang-6.0: error: cannot specify -o when generating multiple output files
    Can anyone help me. I’m in a kind of a hurry

    • AntonMenshov

      I think someone already had this problem in this thread and this was a solution that worked.

  13. Tulio Echeverry

    I did everything as shown here, it worked perfect, then I try include somethings to make opengl work, after that it hasn’t worked again, I undid everything, and it’s still not working
    I am getting this error
    Appel LLVM 9.0 Error
    Command usr/local/opt/llvm/bin/clang failed with exit code 71
    Help Please

    • AntonMenshov

      Hmm, it seems that for some reason XCode again tries to run Apple version of clang instead of the one you’ve just installed. I don’t know why is this happening, but you should definitely dig in that direction.

  14. Abhi

    Worked Perfect! Thumbs Up!

Leave a Reply

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