0

I am trying to run the code below but I keep getting the error 'undefined reference to omp_get_wtime'

`

   #include <omp.h>
   #include <stdio.h>
   static long num_steps = 1024*1024*1024;
   #define MIN_BLK  1024*1024*256

    double pi_comp(int Nstart,int Nfinish,double step)
   {  int i,iblk;
    double x, sum = 0.0,sum1, sum2;
    if (Nfinish-Nstart < MIN_BLK){
       for (i=Nstart;i< Nfinish; i++){
          x = (i+0.5)*step;
          sum = sum + 4.0/(1.0+x*x); 
       }
    }
    else{
       iblk = Nfinish-Nstart;
       sum1 = pi_comp(Nstart,         Nfinish-iblk/2,step);
       sum2 = pi_comp(Nfinish-iblk/2, Nfinish,       step);
       sum = sum1 + sum2;
    }return sum;
    }
     int main ()
     {
     int i;
     double step, pi, sum;
     double init_time, final_time;
     step = 1.0/(double) num_steps;

     init_time = omp_get_wtime();
     sum = pi_comp(0,num_steps,step);
     pi = step * sum;
     final_time = omp_get_wtime() - init_time;
     printf(" for %ld steps pi = %f in %f secs\n",num_steps,pi,final_time);

` I tried making omp_get_wtime into double but it also gives me an error message for the code above.

HotbrewGX
  • 1
  • 1
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Nov 04 '22 at 03:53
  • 1
    You have to link the OpenMP runtime library. Since you do not use openmp pragmas you have 2 options: 1) Use the OpenMP compiler flag (`-fopenmp` on g++ and clang, `/openmp` on MSVC)), or 2) just link the library (`-lgomp` on g++) – Laci Nov 04 '22 at 05:55

0 Answers0