28

I have client server code. LinServer.cpp using pthread to continuously listen client. I created make file to compile all togather:

all: LinServer LinClient

    LinServer:
    g++ LinServer.cpp -o LinServer -pthread

    LinClient:
    g++ LinClient.cpp -o LinClient -pthread

I also tried with -lpthread but same error:

LinServer.cpp:(.text+0x29b): undefined reference to `pthread_create'
LinServer.cpp:(.text+0x2a7): undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
make: *** [LinServer] Error 1

Any idea what's the problem here?

  • to execute make file i use "make" command –  Jun 23 '13 at 20:33
  • 1
    -1 for not using "-Wall" :) – kfsone Jun 23 '13 at 20:37
  • can you please elaborate@ kfsone –  Jun 23 '13 at 20:39
  • the cure in the supposed duplicate is claimed to not work -- now what? – Balog Pal Jun 23 '13 at 20:43
  • @user2500861: Shouldn't your compiler command line to have `-lpthread` (note the letter "L") instead of `-pthread`? – yzt Jun 23 '13 at 20:45
  • @yzt : I already tried with what you said. That also does not make any diff –  Jun 23 '13 at 20:49
  • @user2500861: Try writing the smallest possible program that uses pthreads and see whether this link problem occurs there. If so, then post that code and your compiler command line and then maybe people would be able to help you. Remember that you have to help us help you! – yzt Jun 23 '13 at 20:58

1 Answers1

49

You should use -lpthread not -pthread.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 9
    plus one should make sure the order is correct: it must go in command line after the object file in command line – pmod Jun 23 '13 at 21:46
  • In makefile I was making mistake, I need to include -lpthread that I was missing! –  Jul 05 '13 at 03:05
  • @pmod I tried it and you are right, but I'm curious about why? Thanks. – Zhou Sep 20 '20 at 11:43