Pthread_create() unidentified reference


#1

###Having a Major issue with trying to do a multi-threading project for school

I’m just trying to run sample code to see how pthreads work

> #include <stdio.h>
> #include <stdlib.h>
> #include <pthread.h>
> int sum;
> void *runner(void *param);
> int main(int argc, char *argv[]) {     
>     pthread_t tid;
>     pthread_attr_t attr;
>     
>     if (argc !=2){
>         fprintf(stderr, "usage: a.out <interger value>\n");
>         return -1;
>     }
>     if (atoi(argv[1]) < 0) {
>         fprintf(stderr, "%d must be >= 0\n",atoi(argv[1]));
>         return -1;
>    }
>     pthread_attr_init(&attr);//get default attributes
>     pthread_create(&tid,&attr,runner,argv[1]);//create the thread
>     pthread_join(tid,NULL);//wait for the thread to exit
>     printf("sum = %d\n",sum);
> }

> void *runner(void *param) {
>     int i, upper = atoi(param);
>     sum = 0;
>     for (i =1; i <= upper; i++) {
>         sum += i;
>     } 
>     pthread_exit(0);
> }

And no matter what I do I can’t get the code to compile. I get the following error:

Running “/home/ubuntu/workspace/primefactor/assn3.c”
/tmp/cc58AE5c.o: In function ‘main’:
assn3.c:(.text+0xc4): undefined reference to 'pthread_create’
assn3.c:(.text+0xd5): undefined reference to 'pthread_join’
collect2: error: ld returned 1 exit status
Process exited with code: 1

I’ve tried writing my own makefile with the -pthread flag; didn’t work…
I’ve tried running

sudo apt-get install libpthread-stubs0-dev

to install the right library; didn’t work…

I’m at my wits end to try and get pthreads_create() and pthreads_join() to work and they’re the main requirement for my school project. I’ve already gotten the project working with a normal C program (take numbers from the command line, get the prime factors for each arg, store them in an array, then pass that array and display the results. ex: ./prime.c.o {1…100} would be run from BASH and it outs 1:1 2: 2 3: 3 4: 2 2 etc…
so that’s what the output is supposed to be and I’m supposed to do that with threading, but I can’t do that if I can’t even compile sample code to see how threads work.

Any help would be greatly appreciated in figuring out why I get the above errors.