在 Mac 中使用不同版本的 g++ 运行 OpenMP [英] Running OpenMP with different version of g++ in Mac

查看:61
本文介绍了在 Mac 中使用不同版本的 g++ 运行 OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用 brew 在我的 macOS high Sierra 上安装了 OpenMP.

I recently installed OpenMP on my macOS high Sierra using brew.

我可以使用 g++-9 轻松运行具有 OpenMP 指令的代码(类似于此处答案中的建议:在 Mac OS 上使用 OpenMP 和 C++11 ).但是,我需要将 OpenMP 功能添加到使用 OpenCV 的项目中,并且我只能使用常规 g++ 编译它(g++ —version 显示它是 4.2.1).我不打算使用任何可能使用 OpenMP 的 OpenCV 内置算法,只是想在同一个程序中单独使用它们来优化我自己的一些图像处理算法.如何使 OpenMP 能够与其他版本的 g++ 一起运行?做类似的事情:

I can easily run code that has OpenMP directives using g++-9 (similar to what was suggested in the answer here: Using OpenMP with C++11 on Mac OS ) . However, I need to add OpenMP functionality to a project that uses OpenCV, and I can only compile that with regular g++ ( g++ —version shows it’s 4.2.1) . I do not intend to use any OpenCV built-in algorithms that may use OpenMP, simply want to use them separately in the same program to optimize some image processing algorithms on my own. How can I make OpenMP be able to run with the other version of g++? Doing something like:

g++ hello_world.cpp -fopenmp

给出:

clang: error: unsupported option '-fopenmp'

推荐答案

gccg++ 与 Xcode 一起提供,不过是伪装的 Apple Clang:

gcc and g++ that come with Xcode are nothing but Apple's Clang under disguise:

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
--> Apple clang version 11.0.3 (clang-1103.0.32.59) <--
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Clang 的现代版本确实支持 OpenMP,Apple 的版本也支持.但是:

Modern versions of Clang do support OpenMP, and the one from Apple does too. But:

  1. 它不直接理解 -fopenmp 选项,尽管它列在 clang --help
  2. 的输出中
  3. 它不提供 OpenMP 运行时

有一个解决方法 1.通过 -Xclang -fopenmp 而不是 -fopenmp.但这还不够 - 它会导致编译器无法找到 omp.h 或产生缺少符号的链接错误.这是因为 -Xclang -fopenmp 可以处理和转换 OpenMP pragma,但也需要 OpenMP 运行时.

There is a workaround for 1. Instead of -fopenmp, pass -Xclang -fopenmp. But that is not enough - it will result in either the compiler failing to find omp.h or produce link errors with missing symbols. This is because -Xclang -fopenmp enables processing and transformation of OpenMP pragmas, but one needs the OpenMP runtime too.

对于 2,您可以从 Homebrew 安装 libomp - Intel OpenMP 运行时的开源版本,现在是 LLVM OpenMP 运行时.我想,当您说使用 brew 安装 OpenMP"时,实际上是指您使用 brew 安装了 libomp.

For 2, you may install libomp from Homebrew - the open-source version of Intel OpenMP runtime, which is now LLVM OpenMP runtime. I guess, when you say you "installed OpenMP using brew" you actually mean you installed libomp using brew.

安装 libomp 允许您编译 OpenMP 代码,因为 /usr/local/include(Homebrew 符号链接 omp.h)是在编译器的包含路径上,但您必须显式链接库:

Having libomp installed allows you to compile OpenMP code, because /usr/local/include (where Homebrew symlinks omp.h) is on the compiler's include path, but you have to link the library explicitly:

$ g++ -Xclang -fopenmp foo.cc -lomp

这是 Apple 的 Clang 编译器特有的.通常,当您使用真正的 GCC 或 Clang 来编译和链接代码时,即无需使用链接器链接一堆目标文件的单独步骤,-fopenmp 会同时启用 OpenMP pragma 和还告诉链接器链接 OpenMP 运行时,您不需要指定 -lomp.

This is specific to Apple's Clang compiler. Normally, when you use true GCC or Clang to both compile and link the code, i.e., without a separate step using the linker to link a bunch of object files, -fopenmp enables both processing of OpenMP pragmas and also tells the linker to link the OpenMP runtime and you don't need to specify -lomp.

这篇关于在 Mac 中使用不同版本的 g++ 运行 OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆