Jump to content
  • Advertisement
Sign in to follow this  

Cant get get pthreading working

This topic is 5176 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.
#include <stdio.h>
#include <pthread.h>

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


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!