我是 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)
谢谢阅读。任何帮助表示赞赏。
原文由 user3928256 发布,翻译遵循 CC BY-SA 4.0 许可协议
GCC 4.9.1 通常不随 OS X 一起提供(实际上不再有 GCC 随 Xcode 一起提供)。您的必须是通过其他方式安装的,例如 Homebrew 或自编译,如此 处 所述。 What you are probably missing is properly set
PATH
variable or the additionally installed compiler has version-suffixed binaries, iegcc-4.9
org++-4.9
instead of simplygcc
/g++
。正如@rubenvb 已经提到的,Apple 使用类似 GCC 的名称对 Clang 可执行文件进行符号链接。我个人认为这是一种不好的做法,因为 Xcode 附带的最新 Clang 版本会对无法识别的命令行选项(例如 GCC 前端特定选项)做出反应,并出现硬错误。