Joxixi
Joxixi

Reputation: 731

OpenMP support for Mac using clang or gcc

I am using Mac OS and I created a toy CMake project with OpenMP.

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)

When I tried mkdir build && cd build && cmake .. && make -j to build the project, I got clang: error: unsupported option '-fopenmp'. It works well on Ubuntu. How can I change the CMakeList.txt to enable OpenMP support? Or If the problem can be solved using g++, how to do it?

Thanks for your help!

Upvotes: 0

Views: 1029

Answers (1)

Joxixi
Joxixi

Reputation: 731

I have solved my problem by changing the default compiler to g++. (Note: I have already installed gcc/g++ and the OpenMP library from Homebrew.)

I did not change main.cpp and CMakeList.txt, but when I built the project, I use

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

then the error clang: error: unsupported option '-fopenmp' disappeared. Specifically, I added the option -DCMAKE_CXX_COMPILER=g++-9 to change the default compiler. Maybe the g++ version on your computer is not g++-9, then you can check it under the path /user/local/bin/.

Upvotes: 2

Related Questions