Search code examples
copenmp

How can I resolve omp_get_wtime undefined in C?


I have a program in C with an OpenMP library. I am trying to measure the execution time for a loop to add numbers except that I am getting an error that the reference the function omp_get_wtime() is undefined while I have the library file included in the header section of my code file...

Why am I getting this error and how can I resolve it?

#include <omp.h>
#include <stdlib.h>
#include <string.h>

int main(){
    // Number of threads to use
    int no_threads = 3;

    // Mark the execution go mark
    double start = omp_get_wtime();

    // Parallelize the loop
    #pragma omp parallel num_threads(no_threads)

    for(int i=0; i<1000; i++){
        printf("%d", i);
    }

    double end_time = omp_get_wtime();
    printf("%f", end_time - start);
}

I tried to compile even before I started using the #pragma directive and got the error that the omp function is undefined and the function in the file.


Solution

  • You should build an OMP program with a flag.

    gcc -fopenmp test.c