2

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})
usr1234567
  • 21,601
  • 16
  • 108
  • 128
GeneGi
  • 416
  • 4
  • 11

1 Answers1

3

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

Community
  • 1
  • 1
Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • thank you for your answer! i have added this line to my CMakeList file, but i still got this error **clang: error: unsupported option '-fopenmp'** i know maybe the clion use the default gcc, it needs to be gcc-5, so could you tell me how to modify to make it works, thanks again – GeneGi Apr 28 '16 at 09:00
  • CLion uses GCC set from CMake. So if you use CMake aligned with GCC 5, CLion will use it as well. So yes, just follow the answer above. – nastasiak2512 Apr 28 '16 at 09:56