undefined reference to symbol 'pthread_key_delete@@GLIBC_2.2.5
Asked Answered
S

3

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

Spectrohelioscope answered 2/9, 2014 at 7:17 Comment(1)
I am seeing the same thing, and would appreciate a CMake-oriented answer.Spleenwort
D
24

The above linking problem is solved by adding

-lpthread -lm to CMakeLists.txt (target link libraries for luxrender);
TARGET_LINK_LIBRARIES(... -lpthread -lm)
Duprey answered 20/2, 2015 at 10:49 Comment(2)
Fixed the issue for me, compiling an open source project on a cluster where I have no control over the environment. Cheers.Axe
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.Guenon
G
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

Geostrophic answered 17/1, 2015 at 5:19 Comment(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.Gullett
G
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})
Guenon answered 17/9, 2018 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.