Sign in to follow this  

Cant get get pthreading working

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, im trying to learn about using threads and i made a simple program like one in a tutorial i am reading through. problem is that i cant get it to compile. here is my program.
[source=cpp]
#include <stdio.h>
#include <pthread.h>

void * work( )
{
  int i;
  for( i = 0; i < 11; i++ )
    {
      fprintf( stdout, "%d\n", i );
      usleep( 1000 );
    }

  pthread_exit(0);
  return;
}


int main()
{
  pthread_t t1, t2;

  pthread_create(&t1, 0, work, (void *)0);
  pthread_create(&t2, 0, work, (void *)0);

  pthread_join(t1, 0);
  pthread_join(t2, 0);

  return 0;
}

and here is my makefile multithread.o: multithread.c gcc ${OPT} -c multithread.c multithread: multithread.o gcc ${OPT} -o multithread multithread.o when i go make multithread i get errors saying: gcc -O3 -g -o multithread multithread.o multithread.o(.text+0x17): In function `main': /home/brain/testproj/multithread.c:26: undefined reference to `pthread_create' multithread.o(.text+0x29):/home/brain/testproj/multithread.c:27: undefined reference to `pthread_create' multithread.o(.text+0x36):/home/brain/testproj/multithread.c:29: undefined reference to `pthread_join' multithread.o(.text+0x42):/home/brain/testproj/multithread.c:30: undefined reference to `pthread_join' Any idea why it cant link correctly? i tried doing -l /usr/lib/libpthread.a but that had no effect. Thanks for the help!

Share this post


Link to post
Share on other sites
I beleieve it should just be

gcc -O3 -g -lpthread multithread.o -o multithread

Of course a lot of it depends on how your compiler is set up but in the general case that is how it works. The -l flag automatically adds lib to the front and .a to the end.
So if you did -l/usr/lib/libpthread.a it will become lib/usr/lib/libpthread.a.a

If you want to specify a library directory you use the -L option like
-L/usr/lib -lpthread

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this