24

I'm trying to make a file in Ubuntu and when i make i keep getting this error:

/usr/bin/ld: ../../gtest-1.7.0/libgtest.a(gtest-all.cc.o): undefined reference to symbol     'pthread_key_delete@@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
make[2]: *** [src/tests/run_tests] Error 1
make[1]: *** [src/tests/CMakeFiles/run_tests.dir/all] Error 2
make: *** [all] Error 2

I saw someone mentioning to go into Makefile and adding '-L /lib64 -l pthread' to the variable LDFLAGS but how do you do that? Totally new to linux here =X

vitaut
  • 49,672
  • 25
  • 199
  • 336
imolital
  • 241
  • 1
  • 2
  • 4

3 Answers3

24

The above linking problem is solved by adding

-lpthread -lm to CMakeLists.txt (target link libraries for luxrender);
TARGET_LINK_LIBRARIES(... -lpthread -lm)
user4587644
  • 241
  • 2
  • 3
  • Fixed the issue for me, compiling an open source project on a cluster where I have no control over the environment. Cheers. – domini1000 Mar 12 '17 at 13:19
  • It's only partially correct. If GCC can't link it correctly it has probably also compiled it without thread safe static locals. Adding `-pthread` on both compiler and linker command line should do the trick. – bobah Sep 17 '18 at 15:05
13

I hit the same issue: -lpthread should be last in your linking invocation (has to do with mix of static and shared symbols)

So with CMake: ${CMAKE_THREAD_LIBS_INIT} should be last. For example:

target_link_libraries(mytestlib
  ${BINARY_DIR}/libgmock.a
  glog
  gflags
  ${Boost_LIBRARIES}
  ${CMAKE_THREAD_LIBS_INIT}
)

And for the OP: Search for "thread" in the CMakeLists.txt for the project your are building and paste those section (or link which project you are trying to build if it is open source) - if the above isn't self explanatory

jww
  • 97,681
  • 90
  • 411
  • 885
Laurent Demailly
  • 797
  • 6
  • 10
  • 1
    This is more of a cross-platform appropriate answer. Compiling on Mac vs Linux has different requirements and this prevents any ugly `if (APPLE)` stuff. – David Oct 10 '16 at 20:13
4

If you are building with Make or something else, add -pthread to the compilation command line (so GCC would generate thread-safe static locals) and to the linking command line (so GCC would tell the linker to do the right thing, most notably link with -lpthread).

If you are building with CMake - then most probably you need these (full example):

# always
FIND_PACKAGE(Threads REQUIRED)

# if using boost
SET(Boost_USE_MULTITHREADED ON)

TARGET_LINK_LIBRARIES(my_app ... ${CMAKE_THREAD_LIBS_INIT})
bobah
  • 18,364
  • 2
  • 37
  • 70