C++ 如何在 OS X 中包含 omp.h?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25990296/
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
How to include omp.h in OS X?
提问by user3928256
I'm new in C and have some problems compiling my code in OS X.
我是 C 新手,在 OS X 中编译我的代码时遇到了一些问题。
I code Java a lot both in Eclipse and use terminal to compile my code. However now I'm learning openMP and have troubles with it.
我在 Eclipse 中编写了很多 Java 代码,并使用终端来编译我的代码。但是现在我正在学习 openMP 并且遇到了麻烦。
First I downloaded Xcode to write openMP code but it didn't recognize <omp.h>
. Then I installed g++
. When I type g++ -v
into terminal I get this:
首先,我下载了 Xcode 来编写 openMP 代码,但它无法识别<omp.h>
. 然后我安装了g++
. 当我输入g++ -v
终端时,我得到这个:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
But when I use g++ Mesh.cpp -fopenmp
I still get
但是当我使用g++ Mesh.cpp -fopenmp
我仍然得到
Mesh.cpp:4:10: fatal error: 'omp.h' file not found
#include <omp.h>
^
1 error generated.
Then I tried to install PTP into my Eclipse and got the same problem.
I thought there was no omp.h
in my MacBook so I searched for it and found several omp.h
under folders under gcc-4.9.1/build/
.
然后我尝试将 PTP 安装到我的 Eclipse 中并遇到了同样的问题。我以为omp.h
我的 MacBook中没有,所以我搜索它并omp.h
在gcc-4.9.1/build/
.
Here comes the problem. Based on the Java experience the only reason why I have the file but cannot use it is that the Class Path is wrong. However, I have no idea how to change this configuration in g++, or in Xcode, or in Eclipse. But since I can include files like <stdio.h>
and compile it with all the IDEs, how can't I do the same with <omp.h>
?
问题来了。根据 Java 经验,我拥有该文件但无法使用它的唯一原因是类路径错误。但是,我不知道如何在 g++、Xcode 或 Eclipse 中更改此配置。但是因为我可以包含像这样的文件<stdio.h>
并用所有的 IDE 编译它,我怎么不能用<omp.h>
?
Another thing I noticed is that the gcc folder version is 4.9.1
, but when I type gcc -v
into terminal I get the same with typing in g++ -v
我注意到的另一件事是 gcc 文件夹版本是4.9.1
,但是当我输入gcc -v
终端时,我输入的内容相同g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
Shouldn't the version information says something about 4.9.1
? Just like what java -version
shows
版本信息不应该说明一下4.9.1
吗?就像java -version
显示的那样
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
Thanks for reading. Any help is appreciated.
谢谢阅读。任何帮助表示赞赏。
采纳答案by Hristo Iliev
GCC 4.9.1 normally does not ship with OS X (actually no GCC ships with Xcode any more). Yours must have been installed by another means, e.g. Homebrew or self compilation as described here. What you are probably missing is properly set PATH
variable or the additionally installed compiler has version-suffixed binaries, i.e. gcc-4.9
or g++-4.9
instead of simply gcc
/ g++
.
GCC 4.9.1 通常不随 OS X 一起提供(实际上 GCC 不再随 Xcode 一起提供)。您的必须是通过其他方式安装的,例如 Homebrew 或自编译,如here所述。您可能缺少的是正确设置的PATH
变量,或者额外安装的编译器具有版本后缀的二进制文件,即gcc-4.9
或g++-4.9
代替简单的gcc
/ g++
。
As @rubenvb has already mentioned, Apple symlinks the Clang executables with GCC-like names. I personally find that a bad practice since recent Clang versions shipped with Xcode react on unrecognised command-line options (e.g. GCC frontend specific ones) with hard errors.
正如@rubenvb 已经提到的,Apple 使用类似 GCC 的名称符号链接 Clang 可执行文件。我个人认为这是一个不好的做法,因为最近随 Xcode 一起提供的 Clang 版本会对无法识别的命令行选项(例如 GCC 前端特定选项)做出反应并出现硬错误。
回答by YIMIN TANG
This command can help you
这个命令可以帮助你
brew install libomp
brew 安装 libomp
brew info libomp
libomp: stable 6.0.1 (bottled)
LLVM's OpenMP runtime library
https://openmp.llvm.org/
/usr/local/Cellar/libomp/6.0.1 (12 files, 1.2MB) *
Poured from bottle on 2018-11-20 at 16:12:22
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/libomp.rb
==> Dependencies
Build: cmake ?
==> Requirements
Required: macOS >= 10.10 ?
==> Caveats
On Apple Clang, you need to add several options to use OpenMP's front end
instead of the standard driver option. This usually looks like
-Xpreprocessor -fopenmp -lomp
You might need to make sure the lib and include directories are discoverable
if /usr/local is not searched:
-L/usr/local/opt/libomp/lib -I/usr/local/opt/libomp/include
For CMake, the following flags will cause the OpenMP::OpenMP_CXX target to
be set up correctly:
-DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp -I/usr/local/opt/libomp/include" -DOpenMP_CXX_LIB_NAMES="omp" -DOpenMP_omp_LIBRARY=/usr/local/opt/libomp/lib/libomp.dylib
回答by rubenvb
The gcc
and g++
commands are not what you think they are with XCode: Apple thought it would be a good idea to masquerade Clang as GCC to make the transition smoother.
该gcc
和g++
你认为他们与XCode的命令不是:苹果公司认为这将是伪装锵作为GCC,使过渡更平滑的一个好主意。
Clang OpenMP support is still being worked on. If I didn't miss any big release of the WIP code, you'll need to build this version of clangand use that.
Clang OpenMP 支持仍在开发中。如果我没有错过 WIP 代码的任何大版本,您将需要构建这个版本的 clang并使用它。
You can of course always install a real GCC through stuff like homebrew or macports, that will come with OpenMP support.
当然,您始终可以通过 Homebrew 或 macports 之类的东西来安装真正的 GCC,这些东西会附带 OpenMP 支持。
回答by Freeman
The omp.h
file has been moved to a subdirectory. I found it in MacPorts and solved this compilation problem by creating a link to this file:
该omp.h
文件已移至子目录。我在 MacPorts 中找到了它,并通过创建指向此文件的链接解决了这个编译问题:
sudo ln -s /opt/local/include/libomp/omp.h /opt/local/include/omp.h
回答by Dani Fr??il?
MacOS seems to have that library included but XCode can't find it if you simply use:
MacOS 似乎包含该库,但如果您只是使用,XCode 找不到它:
#include <omp.h>
However, if you don't have the library installed,you can simply add it by installing it with HomeBrew:
但是,如果您没有安装该库,则可以通过使用 HomeBrew 安装来简单地添加它:
brew install libomp
After doing this , simply replace library include code with this one:
完成此操作后,只需将库包含代码替换为以下代码:
#include "/usr/local/opt/libomp/include/omp.h"
or the path that terminal shows you after installing libomp with brew.
或使用 brew 安装 libomp 后终端向您显示的路径。