在CLion中设置OpenMP项目MacOS Mojave



我正试图用macOS Mojave在Mac上用OpenMP在C++中执行我的作业。但它失败了。Mac是新的,所以所有设置都不会更改。我做了什么:

  1. 我安装了Homebrew
  2. 我安装了llvm(brew-install-llvm(
  3. 我安装了omp(brew install libomp(

此外,在项目的CMakeLists.txt中,我有

cmake_minimum_required(VERSION 3.5.1)
project(...)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
set(CMAKE_CXX_COMPILER /usr/local/opt/llvm/bin/clang++)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -fopenmp -O3")
add_executable(...)

在终端"clang-v":

Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

"gcc-v":

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

在CLion中,我有这样的错误:

[ 25%] Linking CXX executable search
ld: unknown option: -platform_version
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [search] Error 1
make[2]: *** [CMakeFiles/search.dir/all] Error 2
make[1]: *** [CMakeFiles/search.dir/rule] Error 2
make: *** [search] Error 2

我重读了很多论坛,但我仍然不明白问题出在哪里

请务必调用Xcode预处理器来处理OMP。

C++标志应包括:-Xpreprocessor -fopenmp -lomp -I/usr/local/include

LDFLAGS应包括:-lomp

使用cmake,您可以将它们添加到您的命令中:cmake -DCMAKE_CXX_FLAGS="-Xpreprocessor -fopenmp -lomp -I/usr/local/include" -DCMAKE_EXE_LINKER_FLAGS="-lomp" ..

或者您可以将它们添加到您的CMakeLists.txt

在尝试了以下链接中的许多选项后,

  • 如何在CLion中建立基本的openMP项目
  • 如何在CMake';s try_compile函数..还有更多

我有错误The CMAKE_CXX_COMPILER: g++-6 is not a full path and was not found in the PATH.所以

  1. 我从brew而不是Clang安装了GNU gcc(gcc@12已安装(
brew install gcc
  1. 将CLion中Preferences > Build, Execution and Development > Toolchains中的c++和c编译器更改为1中已安装的编译器
/usr/local/bin/gcc-12 // c
/usr/local/bin/g++-12 // c++
  1. 此时我的CMakeList.txt文件如下所示:
cmake_minimum_required(VERSION 3.17)
project(test)
set(CMAKE_CXX_STANDARD 11)
add_executable(test main.cpp)
target_compile_options(test PRIVATE -Wall ${OpenMP_CXX_FLAGS})
target_link_libraries(test PRIVATE ${OpenMP_CXX_FLAGS})

它对我有效。

最新更新