C语言 在 Mac OS X 10.11 上安装 OpenMP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35134681/
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
Installing OpenMP on Mac OS X 10.11
提问by WΔ_
How can I get OpenMP to run on Mac OSX 10.11, so that I can execute scripts via terminal?
如何让 OpenMP 在 Mac OSX 10.11 上运行,以便我可以通过终端执行脚本?
I have installed OpenMP: brew install clang-omp.
我已经安装了 OpenMP: brew install clang-omp。
When I run, for example: gcc -fopenmp -o Parallel.b Parallel.cthe following expression returns: fatal error: 'omp.h' file not found
例如,当我运行时:gcc -fopenmp -o Parallel.b Parallel.c以下表达式返回:fatal error: 'omp.h' file not found
I have also tried: brew install gcc --without-multilibbut unfortunately this eventually returned the following (after first installing some dependencies):
我也尝试过:brew install gcc --without-multilib但不幸的是,这最终返回了以下内容(在首先安装了一些依赖项之后):
The requested URL returned error: 404 Not Found
Error: Failed to download resource "mpfr--patch"
Any recommended work arounds?
任何推荐的解决方法?
回答by Alejandro Daniel Noel
On a mac, the command gcc is a symlink to Clang. So by calling gcc -fopenmp -o your_program your_program.cyou are in fact using Clang, which until now has not had built-in support for OpenMP.
在 Mac 上,命令 gcc 是 Clang 的符号链接。因此,通过调用gcc -fopenmp -o your_program your_program.c您实际上是在使用 Clang,它直到现在还没有对 OpenMP 的内置支持。
The newer versions of Clang do have support for OpenMP according to thispost (where you can also find instructions on how to set it up).
根据这篇文章,较新版本的 Clang 确实支持 OpenMP (您还可以在其中找到有关如何设置它的说明)。
On the other hand, if you still want to use gcc I can guide you through the steps that worked for me.
另一方面,如果您仍然想使用 gcc,我可以指导您完成对我有用的步骤。
Install gcc with brew. The command you used should work:
brew install gcc --without-multilibAlternatively, if brew says that you already have gcc installed you can try
brew reinstall gcc --without-multilibAs you may have noted, if you don't specify
--without-multilibbrew warns you that OpenMP may not work.Find the location of the newly installed gcc. Brew appends the version number to gcc so that it does not conflict with the one installed by Command Line Tools. You will find the symlink in
usr/local/bin. In my case it'susr/local/bin/gcc-5. If you right-click and chose "Show original" it should show the gcc-5 executable in/usr/local/Cellar/gcc/5.3.0/bin/gcc-5(version numbers may differ).Now you need to tell your system about it. When calling a compiler your bash will look into
/usr/binby default and not in/usr/local/bin. You need to add this directory to your $PATH. This can be easily done with the command:PATH=/usr/local/bin:$PATHNow you should be able to compile with OpenMP enabled using:
gcc-5 -fopenmp -o your_program your_program.cRemark: gcc-5 is the version I have installed, yours might differ.
用 brew 安装 gcc。您使用的命令应该可以工作:
brew install gcc --without-multilib或者,如果 brew 说您已经安装了 gcc,您可以尝试
brew reinstall gcc --without-multilib正如您可能已经注意到的,如果您不指定
--without-multilibbrew 会警告您 OpenMP 可能无法工作。找到新安装的 gcc 的位置。Brew 将版本号附加到 gcc 以便它不会与命令行工具安装的版本号冲突。您将在
usr/local/bin. 就我而言,它是usr/local/bin/gcc-5. 如果您右键单击并选择“显示原件”,它应该显示 gcc-5 可执行文件/usr/local/Cellar/gcc/5.3.0/bin/gcc-5(版本号可能不同)。现在你需要告诉你的系统。调用编译器时,您的 bash 将
/usr/bin默认查看而不是在/usr/local/bin. 您需要将此目录添加到您的$PATH。这可以使用以下命令轻松完成:PATH=/usr/local/bin:$PATH现在,您应该能够使用以下命令在启用 OpenMP 的情况下进行编译:
gcc-5 -fopenmp -o your_program your_program.c备注:gcc-5 是我安装的版本,你的可能会有所不同。
回答by prakharjain
install clang-omp
brew install clang-ompmake sure you xcode command line tool
xcode-select --installI actually had one error while running a sample openmp code
/usr/local/opt/libiomp/include/libiomp/omp.h:139:21: error: expected ';' after top level declarator extern void __ KAI_KMPC_CONVENTION kmp_set_stacksize_s (size_t);Just remove one space that is present between __ and KAI from the file
Now use the command
clang-omp -fopenmp helloopenmp.cand run the following code
#include <omp.h> #include <stdio.h> int main() { #pragma omp parallel printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads()); }You should get output similar to this
Hello from thread 3, nthreads 4 Hello from thread 2, nthreads 4 Hello from thread 0, nthreads 4 Hello from thread 1, nthreads 4Worked on OS X 10.11.3 and with brew update dated 18th Feb 2016
安装 clang-omp
brew install clang-omp确保您使用 xcode 命令行工具
xcode-select --install我实际上在运行示例 openmp 代码时遇到了一个错误
/usr/local/opt/libiomp/include/libiomp/omp.h:139:21: error: expected ';' after top level declarator extern void __ KAI_KMPC_CONVENTION kmp_set_stacksize_s (size_t);只需从文件中删除 __ 和 KAI 之间的一个空格
现在使用命令
clang-omp -fopenmp helloopenmp.c并运行以下代码
#include <omp.h> #include <stdio.h> int main() { #pragma omp parallel printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads()); }你应该得到类似的输出
Hello from thread 3, nthreads 4 Hello from thread 2, nthreads 4 Hello from thread 0, nthreads 4 Hello from thread 1, nthreads 4在 OS X 10.11.3 和 2016 年 2 月 18 日的 brew 更新上工作
回答by Timothy L.J. Stewart
macOS High Sierra Version 10.13.6 (17G65)
macOS High Sierra 版本 10.13.6 (17G65)
1. Install gcc
1.安装gcc
brew reinstall gcc --without-multilib
brew reinstall gcc --without-multilib
2. Compile
2.编译
gcc-8 -Wall -fopenmp your-parallel-program.c
gcc-8 -Wall -fopenmp your-parallel-program.c
Notice the gcc-8watch the versionbrew installs, yours may be gcc-7or gcc-9
注意gcc-8观察brew 安装的版本,你的可能是gcc-7或gcc-9
==> Pouring gcc-8.2.0.high_sierra.bottle.1.tar.gz
/usr/local/Cellar/gcc/8.2.0: 1,495 files, 344.8MB
==> Pouring gcc-8.2.0.high_sierra.bottle.1.tar.gz
/usr/local/Cellar/gcc/8.2.0: 1,495 files, 344.8MB
That's it!
就是这样!
回答by Javan You
Install gcc using brew.
brew install gccCheck gcc is installed.
$which g++-7 /usr/local/bin/g++-7Change cmake cxx compiler.
cmake -DCMAKE_CXX_COMPILER=g++-7 make
使用 brew 安装 gcc。
brew install gcc检查 gcc 是否已安装。
$which g++-7 /usr/local/bin/g++-7更改 cmake cxx 编译器。
cmake -DCMAKE_CXX_COMPILER=g++-7 make
Maybe this will help you.
也许这会帮助你。
回答by Adrian Leo
brew install cmake
brew install gcc --without-multilib
cmake -DCMAKE_CXX_COMPILER=g++-6 ..
make -j
回答by navins
After install gcc:
安装 gcc 后:
brew install gcc --without-multilib
and export PATH:
并导出路径:
export PATH=/usr/local/bin:$PATH
You may need to export CC, which works for me:
您可能需要导出 CC,这对我有用:
export CC=/usr/local/bin/gcc
This maybe gcc-7, or whatever.
这可能是 gcc-7 或其他。

