33

I just pulled a git repository in which me and my friends are developing an application. When I am running make I am facing this error:

undefined reference to symbol 'pthread_create@@GLIBC_2.2.5' /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status Makefile:182: recipe for target 'bin/release/ns' failed make[1]: * [bin/release/ns] Error 1 Makefile:133: recipe for target 'release' failed make: * [release] Error 2

my friend pulled the same branch and he runs it without any problem.

Could you please give me some hints about the fix? Detailed answer would be highly appreciated.

jww
  • 97,681
  • 90
  • 411
  • 885
Chamran Ashour
  • 441
  • 1
  • 4
  • 6
  • 7
    Does your makefile specify `-pthread` when linking? – David Schwartz Dec 07 '15 at 21:17
  • 7
    [Add `-pthread` to your C++ compiler flags](http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling) (either manually or by [modifying `CXXFLAGS`](http://stackoverflow.com/questions/495598/difference-between-cppflags-and-cxxflags-in-gnu-make)). – набиячлэвэли Dec 08 '15 at 20:37
  • 1
    problem was that . Thnx – Chamran Ashour Dec 10 '15 at 02:32
  • 1
    Possible duplicate of [Strange linking error: DSO missing from command line](https://stackoverflow.com/questions/19901934/strange-linking-error-dso-missing-from-command-line) – perror Jul 18 '17 at 16:45
  • Possible duplicate of [libpthread.so.0: error adding symbols: DSO missing from command line](https://stackoverflow.com/q/19901934/608639) and [Significance of -pthread flag when compiling](https://stackoverflow.com/questions/2127797/significance-of-pthread-flag-when-compiling) – jww Mar 28 '19 at 14:10

2 Answers2

57

I've been working on a multi-platform game engine and I faced same issue only on Linux. If you are using cmake add following to your cmake file:

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

If you are not using cmake you need to add this flag for your compiler manually.

The complete cmake for using threads in linux systems must contain following commands:

      set(CMAKE_THREAD_LIBS_INIT "-lpthread")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
      set(CMAKE_HAVE_THREADS_LIBRARY 1)
      set(CMAKE_USE_WIN32_THREADS_INIT 0)
      set(CMAKE_USE_PTHREADS_INIT 1)
      set(THREADS_PREFER_PTHREAD_FLAG ON)

Note: Also this fix works for Mac-Os but with one difference. You don't need to pass -pthread as compiler flag

Mohammad f
  • 961
  • 10
  • 11
3

You should add "-lpthread" to your library. Of cause, you should also add the directory of libpthread to your library directories.

  • 6
    You should use -pthread for compile and link. It is *not* recommended to use -lpthread! From the manual: -pthread: Link with the POSIX threads library. This option is supported on GNU/Linux targets, most other Unix derivatives, and also on x86 Cygwin and MinGW targets. On some targets this option also sets flags for the preprocessor, so it should be used consistently for both compilation and linking. – Klaus Aug 08 '18 at 08:20
  • 1
    With modern Linux, using `-lpthread` alone results in something like `Undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'`. The `pthread` library is a special case and you should use compiler support for it and drop the lowercase L at the start: `-pthread`. – Mikko Rantalainen Jul 09 '22 at 15:37