在 Mac OS X (sierra & Mojave) 中在 clang 中启用 OpenMP 支持 [英] Enable OpenMP support in clang in Mac OS X (sierra & Mojave)

查看:29
本文介绍了在 Mac OS X (sierra & Mojave) 中在 clang 中启用 OpenMP 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Mac OS X Sierra,我发现 clang(LLVM 版本 8.1.0 (clang-802.0.38))不支持 OpenMP:当我运行 clang -fopenmp program_name.c 时,出现以下错误:

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 got the following error:

clang: 错误:不支持的选项'-fopenmp'

似乎clang不支持-fopenmp标志.

It seems that clang does not support -fopenmp flag.

我在自制软件中找不到任何 openmp 库.根据 LLVM 网站,LLVM 已经支持 OpenMP.但是我在编译过程中找不到启用它的方法.

I could not find any openmp library in homebrew. According to LLVM website, LLVM already supports OpenMP. But I could not find a way to enable it during compiling.

这是否意味着 Mac 中的默认 clang 不支持 OpenMP?你能提供任何建议吗?

Does this mean that the default clang in Mac does not support OpenMP? Could you provide any suggestions?

(当我切换到gcc编译同一个程序时(gcc是用brew install gcc --without-multilib安装的),编译成功.

(When I switch to GCC to compile the same program (gcc is installed using brew install gcc --without-multilib), and the compilation is successful.)

推荐答案

  1. 尝试使用 Homebrew 的 llvm:

brew install llvm

  • 然后,您将拥有 /usr/local/opt/llvm/bin 中的所有 llvm 二进制文件.

  • You then have all the llvm binaries in /usr/local/opt/llvm/bin.

    编译 OpenMP Hello World 程序.把omp_hello.c

    Compile the OpenMP Hello World program. Put omp_hello.c

    /******************************************************************************
         * FILE: omp_hello.c
         * DESCRIPTION:
         *   OpenMP Example - Hello World - C/C++ Version
         *   In this simple example, the master thread forks a parallel region.
         *   All threads in the team obtain their unique thread number and print it.
         *   The master thread only prints the total number of threads.  Two OpenMP
         *   library routines are used to obtain the number of threads and each
         *   thread's number.
         * AUTHOR: Blaise Barney  5/99
         * LAST REVISED: 04/06/05
         ******************************************************************************/
         #include <omp.h>
         #include <stdio.h>
         #include <stdlib.h>
    
         int main (int argc, char *argv[]) 
         {
         int nthreads, tid;
    
         /* Fork a team of threads giving them their own copies of variables */
         #pragma omp parallel private(nthreads, tid)
           {
    
           /* Obtain thread number */
           tid = omp_get_thread_num();
           printf("Hello World from thread = %d
    ", tid);
    
           /* Only master thread does this */
           if (tid == 0) 
             {
             nthreads = omp_get_num_threads();
             printf("Number of threads = %d
    ", nthreads);
             }
    
           }  /* All threads join master thread and disband */
    
         }
    

    在一个文件中并使用:

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

    您可能还需要使用 -I/usr/local/opt/llvm/include 设置 CPPFLAGS.

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

    makefile 应如下所示:

    The 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)
    

  • 更新

    在 macOS 10.14 (Mojave) 中,您可能会收到类似的错误

    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
    

    如果发生这种情况,则 /usr/include 中缺少 macOS SDK 标头.他们使用 Xcode 10 迁移到 SDK 本身.使用

    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
    

    这篇关于在 Mac OS X (sierra &amp; Mojave) 中在 clang 中启用 OpenMP 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆