Patrick Weiss
Patrick Weiss

Reputation: 27

Cannot compile multi-threading example in C

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.

I really have already spent several hours searching, trying, but no luck getting rid of these undefined reference errors for pthread_create() and pthread_join() . Any help would be greatly appreciated in figuring out why I get the above errors.

Edit:

Thanks to those that tried answering for me. I don't know why I got the down vote; I'm not very active here and I do try to be polite when asking for help. Anyways I finally figured it out.

If I am using the make file it had to look like this:

assn3:
    gcc -g -Wall -pthread assn3.c -o assn3

Then from the command line: make assn3

Upvotes: 1

Views: 473

Answers (1)

tdao
tdao

Reputation: 17713

It appears you haven't specified to the linker about thread library. For multithreading applications, the usual option for gcc is follows (assume your program file name thread1.c):

gcc -D_REENTRANT  -lpthread  thread1.c   -o thread1

It's also recommended to use -Wall -O3 -pedantic-errors to be pedantic about compiler warnings etc. So this should do:

gcc -Wall -O3 -pedantic-errors -D_REENTRANT  -lpthread  thread1.c   -o thread1

Upvotes: 1

Related Questions