在Mac OS上使用OpenMP和C ++ 11

时间:2016-10-11 14:38:12

标签: c++ c++11 openmp macos-sierra

我试图在我的C ++ 11代码中使用一些OpenMP多线程功能,如:

#pragma omp parallel for

当我尝试使用编译时:

clang++ -std=c++11 -stdlib=libc++ -fopenmp main.cpp -o main.o

我收到以下错误:

clang: error: unsupported option '-fopenmp'

我或者尝试使用编译:

g++ -fopenmp main.cpp -o main.o

但这会产生完全相同的错误。

如果您可以建议如何在Mac OS 10.12上编译具有OpenMP功能的C ++ 11代码,我将非常感激。关于这里和其他地方的帖子的其他问题解释了为OpenMP编译C代码,但是,我找不到C ++ 11的任何内容。

当我尝试安装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.

如果我使用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 个答案:

答案 0 :(得分:9)

更新了答案

由于我在下面的原始答案,情况有所改善,您可以轻松地将OpenMP与clang++编译器一起使用 - 赶快行动!

要做到这一点,首先使用自制来安装brew install libomp

brew install libomp

然后在使用clang++时,请使用以下标志:

clang++ -Xpreprocessor -fopenmp main.cpp -o main -lomp 

原始答案

如果要在OSX上编译C ++ 11 OpenMP代码,最简单的方法是使用gcc,可以通过homebrew安装。

首先,检查可用选项:

brew options gcc

示例输出

--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

所以,我怀疑你想要:

brew install gcc --without-multilib --without-fortran

安装完成后,您需要确保使用的是homebrew版本,而不是Apple提供的版本。您需要知道homebrew安装/usr/local/bin中的所有内容,并且C ++编译器是g++-6。所以,你需要编译:

/usr/local/bin/g++-6 -std=c++11 -fopenmp main.cpp -o main

或者,在您的登录资料中设置您的PATH:

export PATH=/usr/local/bin:$PATH

那么你可以这样做:

g++-6 -std=c++11 -fopenmp ...

请注意,如果您选择上面的第二个选项(即export PATH=...选项),您还需要在当前会话中键入export命令一次以激活它,或者注销重新登录,因为您的配置文件命令仅在登录时执行。

AFAIK,无需明确安装libiomp - 不确定为什么要这样做。

答案 1 :(得分:2)

XCode附带的Clang不支持OpenMP,但是来自llvm.org的那个。有instructions for how to install it

默认情况下,在OSX上,GCC只是Clang的符号链接(根据Compile OpenMP programs with gcc compiler on OS X Yosemite的评论)。您可以从Homebrew安装真正的GCC,它肯定支持OpenMP。