0
#include<stdio.h>
#include<math.h>
#include<pthread.h>
#include<stdlib.h> 

long double x,fact[150],pwr[150],s[1];
int i,term;

void *Power(void *temp)
{   
    int k;
    for(k=0;k<150;k++)
    {
        pwr[k] = pow(x,k);
    //printf("%.2Lf\n",pwr[k]);
    } 

    return pwr;
}

void *Fact(void *temp)
{
    long double f;
    int j;

    fact[0] = 1.0;

    for(term=1;term<150;term++)
    {
        f = 1.0;
        for(j=term;j>0;j--)
            f = f * j;

        fact[term] = f;
        //printf("%.2Lf\n",fact[term]);
    }

   return fact;
}

void *Exp(void *temp)
{
    int t;

    s[0] = 0;   
    for(t=0;t<150;t++)
        s[0] = s[0] +  (pwr[t] / fact[t]);

    return s;
}

int main(void)
{
    pthread_t thread1,thread2,thread3;  

    printf("Enter the value of x (between 0 to 100) (for calculating exp(x)) : ");
    scanf("%Lf",&x);

    printf("\nThreads creating.....\n");
    pthread_create(&thread1,NULL,Power,NULL); //calling power function
    pthread_create(&thread2,NULL,Fact,NULL);  //calling factorial function
    printf("Threads created\n");

    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);
    printf("Master thread and terminated threads are joining\n");

    printf("Result collected in Master thread\n");

    pthread_create(&thread3,NULL,Exp,NULL);
    pthread_join(thread3,NULL);

    printf("\nValue of exp(%.2Lf) is : %Lf\n\n",x,s[0]);
    exit(1);
}

I was trying to run the above program in linux ubuntu. It is giving following errors

parallelcomp.cpp:(.text+0x1ec): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x207): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x222): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x233): undefined reference to `pthread_join'
parallelcomp.cpp:(.text+0x262): undefined reference to `pthread_create'
parallelcomp.cpp:(.text+0x273): undefined reference to `pthread_join'

The error is mostprobably due to linking binary with pthreads.

Is there any command in ubuntu terminal whcih can solve this problem.? I have tried with several commands given in this community forum, non of them is helpful. Is there anyone who would like to help me? I am also very new to Linux ubuntu. Any kind of suggestion is appreciable. How to include libpthread ?

When I am putting the following command, gcc -pthread -o term term.c, in terminalwe i get the following error: Command line option 'p' [from -pthread] is not known.

Alexey Kukanov
  • 12,479
  • 2
  • 36
  • 55
user3531410
  • 123
  • 1
  • 2
  • 6
  • possible duplicate of [undefined reference to pthread\_create in Linux](http://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux) – Alexey Kukanov May 06 '14 at 10:17

2 Answers2

0

Please try the following -lpthread. Which version of gcc are you using?

Why do I get "undefined reference" errors even when I include the right header files?

Community
  • 1
  • 1
Kajal Sinha
  • 1,565
  • 11
  • 20
0

While compiling add link to pthread library -lpthread

Rocoder
  • 1,083
  • 2
  • 15
  • 26