C++ 启用 openmp 时出错 - “ld: library not found for -lgomp”和 Clang 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20321988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:03:20  来源:igfitidea点击:

Error enabling openmp - "ld: library not found for -lgomp" and Clang errors

c++gccclangopenmposx-mavericks

提问by Davy Li

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

我正在尝试让 openmp 在我的小牛队程序中运行,但是当我尝试使用该标志进行编译时-fopenmp,出现以下错误:

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.

另外,当我运行 gcc 时,我会收到 Clang 警告,我觉得这很奇怪。查看 /usr/bin/gcc 它似乎没有链接到 Clang。

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

关于如何修复我的 Clang 错误并让 openmp 进行编译的任何建议?

采纳答案by Hristo Iliev

The gcccommand 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 tutorialor by using any of the available software package management systems like MacPortsand Homebrew.

gcc最新 Xcode 套件中的命令不再是 LLVM(基于非常旧的 GCC 4.2.1)的 GCC 前端,而是clang. Clang(尚)不支持 OpenMP。您必须单独安装另一个版本的 GCC,例如按照本教程或使用任何可用的软件包管理系统(如MacPortsHomebrew )

回答by Jason Parham

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.

该脚本会将所有内容下载到 ~/code 中以便于维护,并将正确的环境变量附加到您的 ~/.profile 文件中。对于高级用户,选择一个你想要安装 lib、bin 和 include 的好位置,然后手动移动它们。该脚本取决于了解 Intel 的最新 OpenMP 运行时,可以在脚本顶部进行更改。

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.

除了一个小问题外,该脚本应该与 vanilla Mavericks 一起开箱即用。在 OpenML 运行时 make 脚本中,它在指定时不可靠地接受 clang,并继续使用默认的 GCC。因此,如果您没有安装 GCC(这在开箱即用的 Mavericks 上是不正常的),它将无法构建。要解决此问题,您必须根据 OpenMP 的 libomp_20131209_oss.tgz 版本注释掉两行(如脚本中所述)。较新的 OpenML 版本可能会破坏此脚本,因此在较新版本上使用后果自负。

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.

只需将此脚本保存到文件中,运行“chmod +x filename.sh”,然后从终端运行“./filename.sh”。构建LLVM和Clang需要一段时间,所以请耐心等待。

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.

编辑:此脚本很可能会在优胜美地失败,并且在更新到 OSX 10.10 的开发版本后,我在使用内置的 clang2 时遇到了问题。

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"

回答by Anvaka

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

如果您正在运行自制软件,您可以通过调用来解决这个问题:

brew install clang-omp

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

编译器将在clang-omp++name下可用

回答by Raymond

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

刚刚解决了这个问题。这是答案以及如何使其与 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.dylibto the build phases of project (project settings -> Build Phases -> Drag /usr/lib/libiomp5.dylibinto Link Binary with Libraries)

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

  1. https://www.openmprtl.org/download获取最新版本的 openMP 运行时库

  2. 解压并编译它

    mkdir build && cd build && cmake ..
    
  3. 安装它

    sudo cp ./libiomp5.dylib /usr/lib/
    sudo cp ./omp.h /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/
    
  4. 按照http://clang-omp.github.io/上的说明从 Git 中获取 openmp/clang

  5. 编译 openmp/clang

    cd llvm && mkdir build && cd build && ../configure --enable-optimized && make -j
    sudo make install
    
  6. 通常它会将 clang/clang++ 安装到 /usr/local/bin 中,我们需要用我们的版本替换 Apple clang

    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. 在 Xcode 中创建一个项目,使用 clang-openmp 网站上的 Hello World 代码进行测试。创建后,在项目设置中的Custom Compiler Flags -> Other C Flags中添加“-fopenmp”;添加/usr/lib/libiomp5.dylib到项目的构建阶段(项目设置 -> 构建阶段 ->/usr/lib/libiomp5.dylib使用库拖入链接二进制文件)

  8. 它应该工作。Yosemite + Xcode 6 已经过测试。

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

注意:自定义的 clang 不如 Apple 的稳定。如果编译后遇到奇怪的指令错误,请切换回来。