2016-10-11 152 views
7

我想用我的C++代碼11 OpenMP的一些多線程的功能,如:OpenMP的使用與C++ 11在Mac OS

#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,我得到的錯誤:如果我運行使用G ++

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

- 6,基於以下馬克瑟特查的帖子,它工作得很好,謝謝,我卻得到下面的警告,希望瞭解有關如何避免這些有什麼建議:

$ /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 
       ^ ~~~~~~~~~~~~~ 

回答

5

如果您想在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 - 不知道爲什麼你這樣做。

+0

馬克,謝謝!那效果很好。我確實收到了一些警告,我在上面發佈了,可否請您提供一些建議。 – sap

+0

我想這是與你以前的不成功的嘗試相關的一些問題,因爲我從未見過它。我會嘗試'brew rm libiomp --force'並重建你的代碼並重試。如果失敗了,我會嘗試'brew rm gcc --force',然後按照上面的答案再次安裝'gcc'。 –

+0

謝謝,馬克,會做。 – sap

2

附帶的XCode不支持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。

+0

感謝您的回覆,我嘗試安裝libiomp時遇到了錯誤,我更新了原始文章。 – sap

+0

您不需要明確安裝libiomp。事實上,您稱之爲libiomp的事實表明您使用的是舊版本,因爲在最新的LLVM版本中(缺省情況下啓用OpenMP)庫是libomp。 –