0

I am using gcc 4.6.3, and I am trying to write a program that uses asynchronous flow via threads. After some research, I decided that threads.h was best for windows and pthread.h was best for POSIX systems. So I used pthread.h.

After a lot of debugging, I have my code.

#include <pthread.h>
#include <stdio.h>

void *exe(){
  char i;
  for(i = 0; i < 0; i++){
    printf("%c\n", i);
  }
}

int main(){
  long unsigned int *id = (void *)1ul;
  int d = pthread_create(id, NULL, exe, NULL);
}

This code is not intended to be of any use, it is for me to teach myself threading.

The problem is that it throws the error

exit status 1
/file/path.o: In function `main':
main.c:(.text+0x5d): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status

It seems that the pthread_create function is undefined, but I have included the library and it didn't give me any directory not found errors.

Does anyone know why this is happening?

jmpsabisb
  • 182
  • 1
  • 10
forthe
  • 378
  • 3
  • 15

1 Answers1

1

Compile and link with

gcc -pthread main.c
jmpsabisb
  • 182
  • 1
  • 10
  • Unfortunately, I can't try that out for myself, because I'm using an online compiler that doesn't allow custom build options. But I'll give you a vote anyway, thanks! – forthe Mar 28 '18 at 02:59
  • See the suggested duplicate why this answer likely won't work. –  Mar 28 '18 at 03:00