Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

后端 未结 4 1324
长情又很酷
长情又很酷 2020-11-28 09:37

I am using Mac OS X Sierra, and I found that clang (LLVM version 8.1.0 (clang-802.0.38)) does not support OpenMP: when I run clang -fopenmp program_name.c, I go

相关标签:
4条回答
  • 2020-11-28 10:20

    Try using Homebrew's llvm:

    brew install llvm
    

    You then have all the llvm binaries in /usr/local/opt/llvm/bin. To compile the OpenMP Hello World program, for example, type

    /usr/local/opt/llvm/bin/clang -fopenmp -L/usr/local/opt/llvm/lib omp_hello.c -o hello
    

    You might also have to set the CPPFLAGS with -I/usr/local/opt/llvm/include.

    A makefile should look like this:

    CPP = /usr/local/opt/llvm/bin/clang
    CPPFLAGS = -I/usr/local/opt/llvm/include -fopenmp
    LDFLAGS = -L/usr/local/opt/llvm/lib
    
    omp_hello: omp_hello.c
        $(CPP) $(CPPFLAGS) $^ -o $@ $(LDFLAGS)
    

    Update: In macOS 10.14 (Mojave) you might get an error like

    /usr/local/Cellar/llvm/7.0.1/lib/clang/7.0.1/include/omp.h:118:13: fatal error: 'stdlib.h' file not found
    

    If this happens, the macOS SDK headers are missing from /usr/include. They moved into the SDK itself with Xcode 10. Install the headers into /usr/include with

    open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
    
    0 讨论(0)
  • 2020-11-28 10:21

    MacOS Mojave with CMake

    1. Install LLVM with openmp and libomp with brew

       brew update
       brew install llvm libomp
      
    2. add include directories and link directories in CMakeList.txt

       include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
       link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
      
    3. run CMake with the new compilers

       cmake -DCMAKE_C_COMPILER="/usr/local/opt/llvm/bin/clang" -DCMAKE_CXX_COMPILER="/usr/local/opt/llvm/bin/clang++" ..
      

    The clang version is 7.0.1 at time of writing

    0 讨论(0)
  • 2020-11-28 10:24

    Conda-Based Compilation Environment

    Conda uses clang for OSX compilation (umbrella package cxx-compiler), but I hit similar issues with using llvm-openmp and the -fopenmp flag throwing errors. Solution is rather similar to other answers, but I am including here in case others have more exactly this issue.

    Specific solution was to include the Conda environment's include/ directory in the CFLAGS, namely:

    CFLAGS="-I${CONDA_PREFIX}/include"
    

    Note, I also needed to add -lstdc++ -Wl,-rpath ${CONDA_PREFIX}/lib -L${CONDA_PREFIX}/lib when linking, similar to this GitHub Issue.

    0 讨论(0)
  • 2020-11-28 10:29

    Other people have given one solution (using Homebrew llvm). You can also use OpenMP with Apple Clang and Homebrew libomp (brew install libomp). Just replace a command like clang -fopenmp test.c with clang -Xpreprocessor -fopenmp test.c -lomp.

    0 讨论(0)
提交回复
热议问题