I am trying to use some OpenMP multithreading features in my C++11 code like:
我正在尝试在我的c++ 11代码中使用一些OpenMP多线程特性,比如:
#pragma omp parallel for
When I try to compile using:
当我尝试使用:
clang++ -std=c++11 -stdlib=libc++ -fopenmp main.cpp -o main.o
I get the below error:
我得到以下错误:
clang: error: unsupported option '-fopenmp'
I alternatively tried compiling using:
我也尝试过使用:
g++ -fopenmp main.cpp -o main.o
But this gives the exact same error.
但这是完全相同的误差。
Would appreciate very much if you could advise how can I compile C++11 code that has OpenMP features on Mac OS 10.12. The other questions on posts here and elsewhere explain compiling C code for OpenMP, however, I couldn't find anything for C++11.
如果您能告诉我如何在Mac OS 10.12上编译带有OpenMP特性的c++ 11代码,我将不胜感激。关于本文和其他文章的其他问题解释了如何为OpenMP编译C代码,但是,我找不到任何关于c++ 11的内容。
I get an error when I try to install libiomp, I get errors:
当我试图安装libiomp时,会出错:
$ brew install libiomp
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
ansible-cmdb djview4 khard oysttyer shyaml yazpp
asciinema2gif doctl macvim pazpar2 vala you-get
avro-c elixir mailhog python vdirsyncer youtube-dl
bash-git-prompt file-roller meson python3 vim
commandbox glib metaproxy rtv x265
consul-backinator gtkextra offlineimap s-search xonsh
Error: Could not link:
/usr/local/share/doc/homebrew
Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/man/man1/brew.1
Please delete these paths and run `brew update`.
Error: No available formula with the name "libiomp"
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
Error: No formulae found in taps.
If I run using g++-6, based on Mark Setchell's post below, it works fine, thanks, I however get the below warnings, would appreciate any suggestions on how to avoid these:
如果我使用g++-6运行,基于Mark Setchell的文章,效果很好,谢谢,尽管我收到以下警告,如果您能建议我如何避免这些:
$ /usr/local/bin/g++-6 -std=c++11 -fopenmp main.cpp -o main.o
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:4:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:4:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:89:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:89:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:551:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:551:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1432:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1432:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1799:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1799:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1995:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:1995:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2033:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2033:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2425:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2425:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2559:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2559:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2597:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2597:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2672:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2672:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2837:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2837:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2962:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:2962:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3139:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3139:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3442:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3442:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3588:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3588:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3630:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:3630:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:5253:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:5253:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:5452:11: warning: section "__textcoal_nt" is deprecated
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
/var/folders/jr/30r6h4cj4tl48q_cg9bt5f5h0000gn/T//ccq73Zfg.s:5452:11: note: change section name to "__text"
.section __TEXT,__textcoal_nt,coalesced,pure_instructions
^ ~~~~~~~~~~~~~
2 个解决方案
#1
5
Updated Answer
更新后的答案
Since my original answer below, the situation has improved and you can easily use OpenMP with the clang++
compiler - hurraaaay!
由于我最初的回答是,情况已经改善了,您可以轻松地使用OpenMP与clang++编译器- hurraaaay!
To do that, first use homebrew to install brew install libomp
:
要做到这一点,首先使用homebrew安装libomp:
brew install libomp
Then when using clang++
, use these flags:
然后在使用clang++ +时,使用以下标志:
clang++ -Xpreprocessor -fopenmp main.cpp -o main -lomp
Original Answer
原来的答案
If you want to compile C++11 OpenMP code on OSX, the easiest way is to use gcc
which you can install via homebrew
.
如果您想在OSX上编译c++ 11 OpenMP代码,最简单的方法是使用gcc,您可以通过homebrew安装它。
First, check the available options:
首先,检查可用选项:
brew options gcc
Sample Output
样例输出
--with-all-languages
Enable all compilers and languages, except Ada
--with-java
Build the gcj compiler
--with-jit
Build the jit compiler
--with-nls
Build with native language support (localization)
--without-fortran
Build without the gfortran compiler
--without-multilib
Build without multilib support
--HEAD
Install HEAD version
So, I suspect you want:
所以,我猜你想:
brew install gcc --without-multilib --without-fortran
Once you have got it installed, you need to make sure you are using the homebrew
version rather than the one Apple supplies. You need to know that homebrew
installs everything in /usr/local/bin
and that the C++ compiler is g++-6
. So, you either need to compile with:
一旦你安装好了,你需要确保你使用的是自制的版本,而不是苹果的。您需要知道homebrew安装了/usr/local/bin中的所有内容,并且c++编译器是g++-6。因此,您需要使用:
/usr/local/bin/g++-6 -std=c++11 -fopenmp main.cpp -o main
or, set up your PATH in your login profile:
或者,在登录配置文件中设置路径:
export PATH=/usr/local/bin:$PATH
then you can just do:
然后你可以这样做:
g++-6 -std=c++11 -fopenmp ...
Note that if you choose the second option above (i.e. the export PATH=...
option), you will either need to also type the export
command in your current session once to activate it, or log out and log back in since your profile commands are only executed on login.
注意,如果您选择上面的第二个选项(即导出路径=……)选项),您需要在当前会话中输入一次export命令来激活它,或者注销并重新登录,因为您的概要文件命令仅在登录时执行。
AFAIK, there is no need to explicitly install libiomp
- not sure why you did that.
AFAIK,不需要显式地安装libiomp——不知道为什么要这样做。
#2
2
The Clang that comes with XCode doesn't support OpenMP, however the one from llvm.org does. There are instructions for how to install it.
带有XCode的Clang不支持OpenMP,但是来自llvm.org的那一项是支持OpenMP的。有关于如何安装它的说明。
By default on OSX, GCC is just a symlink to Clang (according to commentary on Compile OpenMP programs with gcc compiler on OS X Yosemite). You can install the real GCC from Homebrew which definitely supports OpenMP.
默认情况下,在OSX上,GCC只是Clang的一个符号链接(根据OSX Yosemite上使用GCC编译OpenMP程序的注释)。您可以从Homebrew安装真正的GCC,它绝对支持OpenMP。
#1
5
Updated Answer
更新后的答案
Since my original answer below, the situation has improved and you can easily use OpenMP with the clang++
compiler - hurraaaay!
由于我最初的回答是,情况已经改善了,您可以轻松地使用OpenMP与clang++编译器- hurraaaay!
To do that, first use homebrew to install brew install libomp
:
要做到这一点,首先使用homebrew安装libomp:
brew install libomp
Then when using clang++
, use these flags:
然后在使用clang++ +时,使用以下标志:
clang++ -Xpreprocessor -fopenmp main.cpp -o main -lomp
Original Answer
原来的答案
If you want to compile C++11 OpenMP code on OSX, the easiest way is to use gcc
which you can install via homebrew
.
如果您想在OSX上编译c++ 11 OpenMP代码,最简单的方法是使用gcc,您可以通过homebrew安装它。
First, check the available options:
首先,检查可用选项:
brew options gcc
Sample Output
样例输出
--with-all-languages
Enable all compilers and languages, except Ada
--with-java
Build the gcj compiler
--with-jit
Build the jit compiler
--with-nls
Build with native language support (localization)
--without-fortran
Build without the gfortran compiler
--without-multilib
Build without multilib support
--HEAD
Install HEAD version
So, I suspect you want:
所以,我猜你想:
brew install gcc --without-multilib --without-fortran
Once you have got it installed, you need to make sure you are using the homebrew
version rather than the one Apple supplies. You need to know that homebrew
installs everything in /usr/local/bin
and that the C++ compiler is g++-6
. So, you either need to compile with:
一旦你安装好了,你需要确保你使用的是自制的版本,而不是苹果的。您需要知道homebrew安装了/usr/local/bin中的所有内容,并且c++编译器是g++-6。因此,您需要使用:
/usr/local/bin/g++-6 -std=c++11 -fopenmp main.cpp -o main
or, set up your PATH in your login profile:
或者,在登录配置文件中设置路径:
export PATH=/usr/local/bin:$PATH
then you can just do:
然后你可以这样做:
g++-6 -std=c++11 -fopenmp ...
Note that if you choose the second option above (i.e. the export PATH=...
option), you will either need to also type the export
command in your current session once to activate it, or log out and log back in since your profile commands are only executed on login.
注意,如果您选择上面的第二个选项(即导出路径=……)选项),您需要在当前会话中输入一次export命令来激活它,或者注销并重新登录,因为您的概要文件命令仅在登录时执行。
AFAIK, there is no need to explicitly install libiomp
- not sure why you did that.
AFAIK,不需要显式地安装libiomp——不知道为什么要这样做。
#2
2
The Clang that comes with XCode doesn't support OpenMP, however the one from llvm.org does. There are instructions for how to install it.
带有XCode的Clang不支持OpenMP,但是来自llvm.org的那一项是支持OpenMP的。有关于如何安装它的说明。
By default on OSX, GCC is just a symlink to Clang (according to commentary on Compile OpenMP programs with gcc compiler on OS X Yosemite). You can install the real GCC from Homebrew which definitely supports OpenMP.
默认情况下,在OSX上,GCC只是Clang的一个符号链接(根据OSX Yosemite上使用GCC编译OpenMP程序的注释)。您可以从Homebrew安装真正的GCC,它绝对支持OpenMP。