How to compiler OpenMP program using clang?

My gcc compiles well, but clang fails with the following message:

clang -fopenmp=libomp -o main main.c
main.c:5:10: fatal error: 'omp.h' file not found

I also installed libomp5 package and changed flag to -fopenmp=libomp5 , though it didn't help either:

clang -fopenmp=libomp5 -o main main.c
clang: error: unsupported argument 'libomp5' to option 'fopenmp='
clang: error: unsupported argument 'libomp5' to option 'fopenmp='

these recommendations didn't work.

Would be grateful for hints on installing necessary 16.04 specific packages and passing corresponding flags.


I had the same problem.

sudo apt install libomp-dev

Fixed it with Ubuntu 16.10

//test.c
#include "omp.h"
#include <stdio.h>

int main(void) {
  #pragma omp parallel
  printf("thread %d\n", omp_get_thread_num());
}

Then

clang test.c -fopenmp
./a.out
thread 0
thread 5
thread 2
thread 1
thread 7
thread 3
thread 4
thread 6

Also

clant-3.9 test.c -fopenmp

works.


GCC and Clang use different OpenMP runtime libraries : libgomp and libomp respectivly.

Clang's runtime is the LLVM OpenMP runtime which in turn is based on the Intel OpenMP runtime (which is open source). https://www.openmprtl.org/

On my system GCC installed omp.h at

/usr/lib/gcc/x86_64-linux-gnu/6/include/omp.h

and libomp-dev insalled omp.h at

/usr/include/omp.h

These are different header files which include different function definitions. It may be okay to use either header file for e.g. omp_get_wtime() but in general I think it's probably better to use the header file that corresponds to the runtime that is linked to.


It seems omp.h file doesn't exist in your system PATH. firstly try to locate omp.h file if you don't know where it is:

find / -name 'omp.h' -type f

And then run this command to compile your code:

clang -o main main.c -I/path/to/omp/folder