17

I am trying to use some OpenMP multithreading features in my C++11 code like:

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

I get an error when I try to install libiomp, I get errors:

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

$ /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
                 ^      ~~~~~~~~~~~~~
sap
  • 332
  • 1
  • 3
  • 8

2 Answers2

27

Updated Answer

Since my original answer below, the situation has improved and you can easily use OpenMP with the clang++ compiler - hurraaaay!

To do that, first use homebrew to install brew install libomp:

brew install libomp

Then when using clang++, use these flags:

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.

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:

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

AFAIK, there is no need to explicitly install libiomp - not sure why you did that.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Mark, thanks! that worked well. I do get some warnings, that I posted above, could you please advise on those as well. – sap Oct 12 '16 at 00:36
  • I guess it is some cruft associated with your previous, unsuccessful attempts as I have never seen it. I would try `brew rm libiomp --force` and rebuild your code and try again. If that fails, I would try `brew rm gcc --force` then install `gcc` again as per my answer above. – Mark Setchell Oct 12 '16 at 07:55
  • Thanks, Mark, will do. – sap Oct 12 '16 at 14:33
  • 2
    The current options given by `brew options gcc` don't include `--without-multilib` or `--without-fortran` – Omar Haque Jun 09 '18 at 13:29
  • 1
    @OmarHaque Thank you for pointing that out - I have made an update to my answer. – Mark Setchell Jun 09 '18 at 15:07
  • open ~/.bash_profile on macosx and write down export PATH="~/usr/local/bin:$PATH", this way it will be saved permanently. g++-8 urfile.cpp -fopenmp – Lucky Oct 29 '18 at 21:22
  • `-lomp` solved "Undefined symbols for architecture x86_64" error in my case – likeachamp Dec 24 '21 at 02:56
3

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.

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.

Community
  • 1
  • 1
syazdani
  • 4,760
  • 1
  • 26
  • 35
  • Thanks for your reply, I updated my original post with errors I get when trying to install libiomp. – sap Oct 11 '16 at 15:09
  • You shouldn't need to install libiomp explicitly any longer. Indeed, the fact that you are calling it libiomp shows that you are using an old release, since in the latest LLVM releases (where OpenMP is enabled by default) the library is libomp. – Jim Cownie Oct 12 '16 at 08:29