Compile an OpenMP program with CMake on OS X


I have a very simple openmp hello world program as this:

#include <stdio.h>
#include <omp.h>

int main()
{
#pragma omp parallel
    {
        int id = omp_get_thread_num();
        printf("hello, from %d.\n", id);
    }
    return 0;
}

I can compile it in my terminal with gcc-5 -fopenmp hello.c -o hello.o

But I want to code in CLion, it uses CMake to organize project, when I just click the run button, I will got an error

fatal error: 'omp.h' file not found
#include <omp.h>

I did search in google, and add something into my CMakeLists.txt file

This is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.3)
project(openmp)

OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
    FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fopenmp")

set(SOURCE_FILES hello.c omp.c)
add_executable(openmp ${SOURCE_FILES})
- - Source

Answers

answered 3 year ago Mathieu #1

Your code is C, not C++, so instead of changing CMAKE_CXX_FLAGS, change CMAKE_C_FLAGS:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp")

And set your C compiler to gcc-5:

set(CMAKE_C_COMPILER /your/path/to/gcc-5)

To know gcc-5 path, in a terminal, type which gcc-5

See also: How to specify new gcc path for cmake

comments powered by Disqus