OpenMP 支持 Mac 使用 clang 或 gcc

OpenMP support for Mac using clang or 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/.

路径下查看