0

i copied a simple code to create thread using pthread_create from [Geeks for Geeks][1]

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for details.
#include <pthread.h>

// A normal C function that is executed as a thread
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}

int main()
{
    pthread_t thread_id;
    printf("Before Thread\n");
    pthread_create(&thread_id, NULL, myThreadFun, NULL);
    pthread_join(thread_id, NULL);
    printf("After Thread\n");
    exit(0);
}

but when I run it in my machine( Ubuntu 20.04 ) using command gcc 04_1.c -o 04_1(04_1 is file name) it gives error like given below..

/usr/bin/ld: /tmp/ccDru27X.o: in function `main':
04_1.cpp:(.text+0x6d): undefined reference to `pthread_create'
/usr/bin/ld: 04_1.cpp:(.text+0x7e): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status

Can anyone guide where it is going wrong? and how to solve this? [1]: https://www.geeksforgeeks.org/multithreading-c-2/

ks1322
  • 31,484
  • 13
  • 100
  • 154
joy
  • 47
  • 8

0 Answers0