在Mac OS X(Sierra& amp; Mojave)的clang中启用OpenMP支持 [英] Enable OpenMP support in clang in Mac OS X (sierra & Mojave)
问题描述
我使用的是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来编译同一程序时(使用 brew install gcc --without-multilib
安装gcc),并且编译成功.)
(When I switch to GCC to compile the same program (gcc is installed using brew install gcc --without-multilib
), and the compilation is successful.)
推荐答案
尝试使用 Homebrew 的llvm:>
Try using Homebrew's llvm:
brew install llvm
然后,您将所有llvm二进制文件保存在/usr/local/opt/llvm/bin
中.例如,要编译 OpenMP Hello World程序,请键入
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
您可能还必须使用 -I/usr/local/opt/llvm/include
设置 CPPFLAGS
.
You might also have to set the CPPFLAGS
with -I/usr/local/opt/llvm/include
.
makefile应该如下所示:
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)
更新:在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
更新2:样本文件 omp_hello.c
似乎已从上面的直接链接中删除,所以它是:
Update 2: The sample file omp_hello.c
seems to be gone from the direct link above, so here it is:
/******************************************************************************
* 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\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
} /* All threads join master thread and disband */
}
这篇关于在Mac OS X(Sierra& amp; Mojave)的clang中启用OpenMP支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!