LoginSignup
3
4

More than 3 years have passed since last update.

Apple ClangでOpenMPをclionから使う

Last updated at Posted at 2020-05-13

MacOS上でApple clangでOpenMPを使うコードをコンパイルする方法が以下の記事に書いてある。
Apple ClangでOpenMPを使う
上記の記事にも書いてある通り、長らくApple clangはopenmpをサポートしておらず、llvm-ompをインストールするといった方法が取られていた。
しかし、2020年時点ではコンパイラをインストールする必要はなく、ランタイムライブラリをインストールすれば使えるようになる。

brew install libomp

コンパイルも上記の記事の通り、簡単に実行できる。

clang -Xpreprocessor -fopenmp -lomp myfile.cxx

cmakeを使う場合、cmake 3.12以降であればfind_package(OpenMP)とするだけで、OpenMPに必要な設定が実行される。

cmake_minimum_required(VERSION 3.12)
project(openmptest CXX)

add_executable(sample sample.cpp)

find_package(OpenMP REQUIRED)
target_link_libraries(sample PRIVATE OpenMP::OpenMP_CXX)

ここまでは非常に簡単にできる。しかし、これをclionからビルドしようとすると以下のようなエラーが出てしまう。

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/murase/work/econo_sim_inoue/Fugaku
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at /Applications/CLion.app/Contents/bin/cmake/mac/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
Call Stack (most recent call first):
  /Applications/CLion.app/Contents/bin/cmake/mac/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /Applications/CLion.app/Contents/bin/cmake/mac/share/cmake-3.15/Modules/FindOpenMP.cmake:477 (find_package_handle_standard_args)
  CMakeLists.txt:6 (find_package)

詳細な原因はわからないが、clionに同梱されているcmakeを使っていることが原因のようだ。

この問題を回避するにはclionからでも、/usr/local/bin/cmakeを使うようにすれば良い。(cmakeは事前にhomebrewでインストールしておく)
"Preferences" -> "Build,execution,deployment" -> "Toolchains" と選択し、cmakeのパスを以下のように設定する。

image.png

その上で、[Tools] -> [CMake] -> [Reste Cache and Reload Project] とすればちゃんとOpenMPを認識してくれるようになる。

参考:

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4