如何在OS X中包含omp.h?

问题描述 投票:16回答:4

我是C的新手,在OS X中编译代码时遇到了一些问题。

我在Eclipse中编写了很多Java代码,并使用终端编译我的代码。但是现在我正在学习openMP并且遇到麻烦。

首先我下载了​​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

但是当我使用g++ Mesh.cpp -fopenmp时,我仍然会得到

Mesh.cpp:4:10: fatal error: 'omp.h' file not found
#include <omp.h>
         ^
1 error generated.

然后我尝试将PTP安装到Eclipse中并遇到同样的问题。我以为我的MacBook中没有omp.h所以我搜索了它,并在omp.h下的文件夹下找到了几个gcc-4.9.1/build/

这就是问题所在。根据Java经验,我拥有该文件但无法使用它的唯一原因是类路径错误。但是,我不知道如何在g ++,Xcode或Eclipse中更改此配置。但是因为我可以包含像<stdio.h>这样的文件并用所有的IDE编译它,我怎么能用<omp.h>做同样的事情呢?

我注意到的另一件事是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

版本信息不应该说有关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)

谢谢阅读。任何帮助表示赞赏。

c++ gcc g++ openmp
4个回答
13
投票

GCC 4.9.1通常不附带OS X(实际上没有GCC随Xcode一起发货)。您的必须通过其他方式安装,例如如here描述的自制或自编。您可能缺少的是正确设置PATH变量或额外安装的编译器具有版本后缀的二进制文件,即gcc-4.9g++-4.9而不是简单的gcc / g++

正如@rubenvb已经提到的那样,Apple使用类似GCC的名称对Clang可执行文件进行符号链接。我个人发现,自Xcode附带的最新Clang版本以来,一些不好的做法会对无法识别的命令行选项(例如GCC前端特定版本)产生严重错误。


24
投票

这个命令可以帮到你

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

12
投票

gccg++命令并不是你认为它们与XCode的关系:Apple认为将Clang伪装成GCC以使转换更顺畅是一个好主意。

Clang OpenMP支持仍在进行中。如果我没有错过WIP代码的任何重大版本,您将需要构建this version of clang并使用它。

你当然可以通过自制软件或macports之类的东西来安装真正的GCC,这些东西将随OpenMP支持一起提供。


3
投票

omp.h文件已移至子目录。我在MacPorts中找到它并通过创建此文件的链接解决了这个编译问题:

sudo ln -s /opt/local/include/libomp/omp.h /opt/local/include/omp.h
© www.soinside.com 2019 - 2024. All rights reserved.