C++ 在 OS X Yosemite 上使用 gcc 编译器编译 OpenMP 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29057437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:08:34  来源:igfitidea点击:

Compile OpenMP programs with gcc compiler on OS X Yosemite

c++cxcodegccopenmp

提问by Tejas Belvalkar

$ gcc 12.c -fopenmp
12.c:9:9: fatal error: 'omp.h' file not found
#include<omp.h>
    ^
1 error generated.

While compiling openMP programs I get the above error. I am using OS X Yosemite. I first tried by installing native gcc compiler by typing gcc in terminal and later downloaded Xcode too still I got the same error. Then I downloaded gcc through:

编译 openMP 程序时出现上述错误。我正在使用 OS X 优胜美地。我首先尝试通过在终端中键入 gcc 来安装本机 gcc 编译器,然后也下载了 Xcode,但我仍然遇到相同的错误。然后我通过以下方式下载了gcc:

$ brew install gcc

Still I'm getting the same error. I did try changing the compiler path too still it shows:

我仍然遇到同样的错误。我也尝试更改编译器路径仍然显示:

$ which gcc
/usr/bin/gcc

So how do I compile programs with gcc?

那么如何用gcc编译程序呢?

回答by IKavanagh

EDIT:As of 13 Aug 2017the --without-multiliboption is no longer present in Homebrew and should not be used. The standard installation

编辑:截至2017 年 8 月 13 日,--without-multilib选项不再出现在 Homebrew 中,不应使用。标准安装

brew install gcc

will provide a gccinstallation that can be used to compile OpenMP programs. As below it will be installed into /usr/local/binas gcc-<version>. The current gcc version available from Homebrew (as of writing) will install as gcc-8. You can compile programs with OpenMP support using it via

将提供gcc可用于编译 OpenMP 程序的安装。如下所示,它将被安装到/usr/local/binas 中gcc-<version>。Homebrew 提供的当前 gcc 版本(撰写本文时)将安装为gcc-8. 您可以通过使用它来编译具有 OpenMP 支持的程序

gcc-8 -fopenmp hello.c

Alternatively you could put an alias in your .bashrcfile as

或者,您可以在.bashrc文件中放置一个别名作为

alias gcc='gcc-8'

and then compile using

然后编译使用

gcc -fopenmp hello.c


Note: I'm leaving the original post here in case it is useful to somebody.

注意:我将原帖留在这里,以防它对某人有用。

The standard gcc available on OS X through XCode and Clang doesn't support OpenMP. To install the Homebrew version of gcc with OpenMP support you need to install it with

通过 XCode 和 Clang 在 OS X 上可用的标准 gcc 不支持 OpenMP。要安装具有 OpenMP 支持的 Homebrew 版本的 gcc,您需要安装它

brew install gcc --without-multilib

or as pointed out by @Mark Setchell

或者正如@Mark Setchell所指出的那样

brew reinstall gcc --without-multilib

This will install it to the /usr/local/bindirectory. Homebrew will install it as gcc-<version>so as not to clobber the gcc bundled with XCode.

这会将它安装到/usr/local/bin目录中。Homebrew 将安装它,gcc-<version>以免破坏与 XCode 捆绑在一起的 gcc。

回答by Tejas Belvalkar

I finally did some research and I finally came across a solution here: <omp.h> library isn't found in the GCC version (4.2.1) in Mavericks.

我终于做了一些研究,我终于在这里找到了一个解决方案:<omp.h> 库不在小牛队的 GCC 版本 (4.2.1) 中找到

  1. I got a new gcc complier from http://hpc.sourceforge.net/
  2. Then I placed a new executable folder by $ sudo tar -xvf gcc-4.9-bin.tar -C /
  3. Later I switched to it by export PATH=/usr/local/bin:$PATHthat seemed to do the trick!
  1. 我从http://hpc.sourceforge.net/得到了一个新的 gcc 编译器
  2. 然后我放置了一个新的可执行文件夹 $ sudo tar -xvf gcc-4.9-bin.tar -C /
  3. 后来我切换到它 export PATH=/usr/local/bin:$PATH,似乎可以解决问题!