OpenMP支持Mac使用clang或gcc



我使用Mac OS,我用OpenMP创建了一个玩具CMake项目。

main.cpp:

#include <iostream>
#include "omp.h"
using namespace std;
int main() {
cout << "Hello, World!" << endl;
#pragma omp parallel
cout << "Hello OpenMP from thread " << omp_get_thread_num() << endl;
return 0;
}

CMakeList.txt:

cmake_minimum_required(VERSION 3.12)
project(omp_test)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
add_executable(omp_test main.cpp)

当我尝试mkdir build && cd build && cmake .. && make -j来构建项目时,我得到了clang: error: unsupported option '-fopenmp'。它在Ubuntu上运行良好。如何更改CMakeList.txt以启用OpenMP支持?或者如果问题可以用g++解决,该怎么做?

谢谢你的帮助!

通过将默认编译器更改为g++,我已经解决了这个问题。(注意:我已经从Homebrew安装了gcc/g++和OpenMP库。)

我没有改变main.cppCMakeList.txt,但是当我构建项目时,我使用

mkdir build && cd build && cmake -DCMAKE_CXX_COMPILER=g++-9 .. && make -j

则错误clang: error: unsupported option '-fopenmp'消失。具体来说,我添加了选项-DCMAKE_CXX_COMPILER=g++-9来更改默认编译器。也许您的计算机上的g++版本不是g++-9,那么您可以在/user/local/bin/路径下检查它。

最新更新