Error enabling openmp - “ld: library not found for

2019-09-05 00:38发布

问题:

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?

回答1:

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.



回答2:

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"


回答3:

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



回答4:

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