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?

7
  • to execute make file i use "make" command Commented Jun 23, 2013 at 20:33
  • 1
    -1 for not using "-Wall" :) Commented Jun 23, 2013 at 20:37
  • can you please elaborate@ kfsone Commented Jun 23, 2013 at 20:39
  • the cure in the supposed duplicate is claimed to not work -- now what? Commented Jun 23, 2013 at 20:43
  • @user2500861: Shouldn't your compiler command line to have -lpthread (note the letter "L") instead of -pthread? Commented Jun 23, 2013 at 20:45

1 Answer 1

50

You should use -lpthread not -pthread.

Sign up to request clarification or add additional context in comments.

3 Comments

plus one should make sure the order is correct: it must go in command line after the object file in command line
In makefile I was making mistake, I need to include -lpthread that I was missing!
@pmod I tried it and you are right, but I'm curious about why? Thanks.