33

I'm trying to get openmp to run in my program on Mavericks, however when I try to compile using the flag -fopenmp I get the following error:

ld: library not found for -lgomp
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The command I am running is:

gcc myProgram.cpp -fopenmp -o myProgram

Also, when I run gcc I get Clang warnings which I find to be very strange. And looking into /usr/bin/gcc it does not appear to link to Clang.

Any suggestions on how to fix my Clang errors and get openmp to compile?

Davy Li
  • 603
  • 2
  • 9
  • 16
  • Are you sure it is `gcc`? Could you check `gcc --version`? – keltar Dec 02 '13 at 09:07
  • Yea, I'm pretty sure: $gcc --version Configured with: -- prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx- include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix` – Davy Li Dec 02 '13 at 17:05
  • Version format resembles the one clang uses, and it mentions LLVM and clang; maybe dragonegg behaves that way, but i doubt that (never seen it - sorry). Unfortunately i have no experience with osx to say more than simple fact that clang doesn't work with openmp yet - and its involvement appears to be a root of your problem. Anyway, if `--version` doesn't says that it is FSF's gcc - then i think it isn't. – keltar Dec 02 '13 at 17:29
  • 1
    Related, for compilers that support OpenMP, here's the 3 second tutorial: [How to Compile and Run an OpenMP Program](https://www.dartmouth.edu/~rc/classes/intro_openmp/compile_run.html). – jww Jun 11 '15 at 21:41

4 Answers4

27

The gcc command in the latest Xcode suite is no longer the GCC frontend to LLVM (based on the very old GCC 4.2.1) but rather a symlink to clang. Clang does not (yet) support OpenMP. You have to install separately another version of GCC, e.g. by following this tutorial or by using any of the available software package management systems like MacPorts and Homebrew.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • 2
    I've used brew to install gcc49 [`brew install gcc49`], but it still seems like gcc is being linked to Clang. `$g++ --version Configured with: -- prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix` Do I have to somehow manually set the symlink for gcc to use gcc 4.9 instead of clang? – Davy Li Dec 03 '13 at 02:59
  • 1
    Homebrew installs software in `/usr/local`. Make sure that `/usr/local/bin` is in your `$PATH` and that it precedes `/usr/bin`. Also it is possible that the GCC binaries that come from Homebrew are suffixed with the version, e.g. `gcc-4.9` instead of simply `gcc`. – Hristo Iliev Dec 03 '13 at 08:16
  • 2
    Is it still true that "Clang does not support OpenMP". See this: http://clang-omp.github.io If I install this, do you think it is going to upset Xcode 5? – RawMean Mar 02 '14 at 07:35
  • @RawMean, did you have any success building and running OpenMP/Clang? – Hristo Iliev Jun 05 '14 at 07:34
  • Yes, compiled and built it. But I still have trouble telling Qt where the libraries are – RawMean Jun 05 '14 at 19:19
13

I just recently attacked this problem and have scripted the process of getting everything working based on the official instructions.

The script will download everything into ~/code for easy maintenance and will append the correct environment variables to your ~/.profile file. For advanced users, pick a nice location you want the lib, bin and include installed and move them manually. The script depends on knowing the latest OpenMP runtime from Intel, which can be altered at the top of the script.

The script should work out of the box with vanilla Mavericks, except for one small problem. In the OpenML runtime make script, it does not reliably accept clang when specified and continues with the default GCC. As such, if you don't have GCC installed (which is not normal on out of the box Mavericks), it will fail to build. To fix this, you must comment out two lines (as noted in the script) based on the libomp_20131209_oss.tgz build of OpenMP. Newer builds of OpenML might break this script, so use at your own peril on newer versions.

Simply save this script into a file, run 'chmod +x filename.sh', and run './filename.sh' from terminal. It will take a while to build LLVM and Clang, so be patient.

EDIT: This script will most likely fail on Yosemite and I am having issues using the built clang2 after the update to the dev builds of OSX 10.10.

INTEL_OPENMP_LATEST_BUILD_LINK=https://www.openmprtl.org/sites/default/files/libomp_20131209_oss.tgz
DEST_FOLDER = ~/code
CLANG_INCLUDE=${DEST_FOLDER}/llvm/include
CLANG_BIN=${DEST_FOLDER}/llvm/build/Debug+Asserts/bin
CLANG_LIB=${DEST_FOLDER}/llvm/build/Debug+Asserts/lib
OPENMP_INCLUDE=${DEST_FOLDER}/libomp_oss/exports/common/include
OPENMP_LIB=${DEST_FOLDER}/libomp_oss/exports/mac_32e/lib.thin

mkdir ${DEST_FOLDER}
cd ${DEST_FOLDER}
git clone https://github.com/clang-omp/llvm
git clone https://github.com/clang-omp/compiler-rt llvm/projects/compiler-rt
git clone -b clang-omp https://github.com/clang-omp/clang llvm/tools/clang
cd llvm
mkdir build
cd build
../configure
make
cd Debug+Asserts/bin
mv clang clang2
rm -rf clang++
ln -s clang2 clang2++
echo "LLVM+Clang+OpenMP Include Path : " ${CLANG_INCLUDE}
echo "LLVM+Clang+OpenMP Bin Path     : " ${CLANG_BIN}
echo "LLVM+Clang+OpenMP Lib Path     : " ${CLANG_LIB}

cd ${DEST_FOLDER}
curl ${INTEL_OPENMP_LATEST_BUILD_LINK} -o libomp_oss_temp.tgz
gunzip -c libomp_oss_temp.tgz | tar xopf -
rm -rf libomp_oss_temp.tgz
cd libomp_oss

echo "You need to do one or two things:"
echo "1.) [Required] Comment out line 433 from libomp_oss/src/makefile.mk"
echo "2.) [Optional] If you do not have GCC installed (not normal on vanilla Mavericks), you must comment out lines 450-451 in libomp_oss/tools/check-tools.pl.  Have you done this or want to compile anyway?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make compiler=clang; break;;
        No ) exit;;
    esac
done

echo "OpenMP Runtime Include Path : " ${OPENMP_INCLUDE}
echo "OpenMP Runtime Lib Path     : " ${OPENMP_LIB}

(echo 'export PATH='${CLANG_BIN}':$PATH';
    echo 'export C_INCLUDE_PATH='${CLANG_INCLUDE}':'${OPENMP_INCLUDE}':$C_INCLUDE_PATH'; 
    echo 'export CPLUS_INCLUDE_PATH='${CLANG_INCLUDE}':'${OPENMP_INCLUDE}':$CPLUS_INCLUDE_PATH';
    echo 'export LIBRARY_PATH='${CLANG_LIB}':'${OPENMP_LIB}':$LIBRARY_PATH';
    echo 'export DYLD_LIBRARY_PATH='${CLANG_LIB}':'${OPENMP_LIB}':$DYLD_LIBRARY_PATH}') >> ~/.profile
source ~/.profile

echo "LLVM+Clang+OpenMP is now accessible through [ clang2 ] via terminal and does not conflict with Apple's clang"
Jason Parham
  • 466
  • 6
  • 10
  • Hi, tried to use this script but I cant find this file: ~/code/libomp_oss/exports/mac_32e/lib.thin I checked, and it indeed is missing, in fact I downloaded the tgz manually and the export directory has a lot of folders but no files – Kenny D. Apr 25 '14 at 01:28
  • It could be putting the libiomp5.dylib in another folder. I'm not super sure why there exists all of the different folders in ../exports/ but I'd try looking in the other folders parallel to mac_32e for the library – Jason Parham Apr 25 '14 at 18:45
  • I am getting the following error with the script above in osx mavericks. – Ramakrishnan Kannan May 05 '14 at 11:56
  • @JasonParham Trying the script, but it results in an error -> any ideas? Thanks: check-tools.pl: (!) Cannot parse GNU compiler version: check-tools.pl: (!) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) ... check-tools.pl: (!) Cannot parse GNU compiler version: check-tools.pl: (!) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) check-tools.pl: (!) Target: x86_64-apple-darwin13.2.0 check-tools.pl: (!) Thread model: posix check-tools.pl: (!) (eof) ../../tools/src/common-checks.mk:69: *** Development tools not found: gcc, g++. Stop. – songololo Jun 26 '14 at 15:09
  • echo "If you do not have GCC installed (not normal on vanilla Mavericks), you must comment out lines 450-451 in libomp_oss/tools/check-tools.pl. Have you done this or want to compile anyway?" There is a bug in the libomp_oss installation script where it will fail to parse the fact that you have a compatible machine. You have two options: 1.) [install GCC](http://leviathan37.tumblr.com/post/76618063571/how-to-install-command-line-tools-on-osx-mavericks) 2.) Do the above commenting as recommended in the script – Jason Parham Jun 27 '14 at 12:59
  • @JasonParham awesome answer. thanks! Just ran into a bit of issues building openMP since unrecognized linker options are now treated as hard errors (`clang: error: unknown argument: '-no-intel-extensions' [-Wunused-command-line-argument-hard-error-in-future] clang: note: this will be a hard error (cannot be downgraded to a warning) in the future`). My hack was to comment out line 433 in "src/makefile.mk". This removes the unrecognized option. – Randi Aug 24 '14 at 15:56
  • I created a Gist of this for others: https://gist.github.com/palmerc/59115c29aa5703f669fc – Cameron Lowell Palmer Aug 25 '14 at 10:39
  • After compiling, it looks like libstdc++ is not included. Should this be compiled as well or will the libraries available in the 'original' clang work? The reason I'm asking, is that clang2++ seems to not handle the original library (found in /usr/lib) very well. – gustafbstrom Oct 14 '14 at 13:16
  • @Randi Thank You, I've incorporated this into the script – Jason Parham Dec 31 '14 at 16:52
  • 1
    @JasonParham I followed the instruction and I managed to install clang. But unfortunately I cannot compile simple hello world. I got this error: $ clang2++ hello.cpp hello.cpp:1:10: fatal error: 'iostream' file not found Could you tell me how can I fix this? – Kris Jan 11 '15 at 14:03
  • 1
    @Kris Whenever I build my projects with Clang2, I explicitly have to include the stdc++ libs. For example in CMake, I add the following lines: set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -stdlib=libstdc++") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lstdc++ -stdlib=libstdc++") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++ -stdlib=libstdc++") – Jason Parham Jan 15 '15 at 07:40
  • Just in case it helps anyone - I managed to get this working on Yosemite by following pretty much the same method, albeit with a newer version of the OpenMP runtime. I had to remove some more lines from the makefile.mk - pretty much anywhere you see the _-static-intel_ or _-no-intel-extensions_ flags, comment out or remove them.I also had to make some changes to one of the Perl scripts - more details are here. http://www.mooseframework.org/wiki/buildingredistributable/rpm_redist/rpm_redist-install/intelopenmp/ – Joel Auterson Mar 03 '15 at 12:45
  • @JasonParham I didn't understand your reply to Kris. I'm having the same problem he was having, and I have no clue on how to fix it! Could you please be more specific? – ccoutinho May 10 '15 at 16:42
4

If you are running homebrew you can fix this problem by calling:

brew install clang-omp

The compiler will be available under clang-omp++ name

Anvaka
  • 15,658
  • 2
  • 47
  • 56
  • 4
    I get `Error: No available formula with the name "clang-omp"` – dispatchswift Sep 05 '16 at 23:48
  • I faced the same issue and cannot install clang-omp through homebrew, but later I found a solution from http://stackoverflow.com/questions/38971394/brew-install-clang-omp-not-working. It helps me install clang-omp successfully. – pitfall Jan 05 '17 at 00:24
  • 1
    You need to install and configure llvm for use. clang-omp has been merged in there. See http://stackoverflow.com/a/39843038/1842416 for details. – elec3647 Apr 14 '17 at 19:56
3

Just worked through this problem. Here's the answer plus how to get it worked with Xcode.

  1. Grab the latest version of openMP runtime library from https://www.openmprtl.org/download

  2. unzip and compile it by

     mkdir build && cd build && cmake .. && make && sudo make install 
    
  3. install it by

     sudo cp ./libiomp5.dylib /usr/lib/
     sudo cp ./omp.h /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/
    
  4. Grab openmp/clang from Git following the instructions on http://clang-omp.github.io/

  5. compile openmp/clang

     cd llvm && mkdir build && cd build && ../configure --enable-optimized && make -j
     sudo make install
    
  6. normally it would install clang/clang++ into /usr/local/bin, we need replace the Apple clang with our version

     cd /usr/bin
     sudo mv clang clang-apple
     sudo mv clang++ clang++-apple
     sudo ln -s /usr/local/bin/clang ./clang
     sudo ln -s /usr/local/bin/clang++ ./clang++
     cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
     sudo mv clang clang-apple
     sudo mv clang++ clang++-apple
     sudo ln -s /usr/local/bin/clang ./clang
     sudo ln -s /usr/local/bin/clang++ ./clang++
     cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
     sudo mv -f * ../../
    
  7. Create a project in Xcode, using the Hello World code on clang-openmp website for test. After created, add "-fopenmp" to Custom Compiler Flags -> Other C Flags in project settings; add /usr/lib/libiomp5.dylib to the build phases of project (project settings -> Build Phases -> Drag /usr/lib/libiomp5.dylib into Link Binary with Libraries)

  8. It should work. Yosemite + Xcode 6 is tested.

Note: the custom clang is NOT as stable as Apple's. Switch back if you meet strange instruction error after compiled.

Dr No
  • 3
  • 3
Raymond
  • 41
  • 4