Undefined Reference to 'Pthread_Key_Create' (Linker Error)

undefined reference to `pthread_key_create' (linker error)

The option -lgtest is attempting to link the dynamic library libgtest.so. You
wish to link the static library /home/user/gtest-1.7.0/lib/.libs/libgtest.a.

Instead of:

g++ main.cpp -I/home/user/gtest-1.7.0/include -L/home/user/gtest-1.7.0/lib/.libs -lgtest -pthread

use:

g++ main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread

Note that your commandline supplies no name for the resulting executable, which will default
to a.out. If you want it called, e.g. mytest, then do:

g++ -o mytest main.cpp -I/home/user/gtest-1.7.0/include /home/user/gtest-1.7.0/lib/.libs/libgtest.a -pthread

undefined reference to `pthread_getspecific' when using cmake for googletest

There is a problem at link-time.

It is only an assumption: did you try this?

target_link_libraries(test gmock gtest pthread)

instead of your version:

target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)

undefined reference to `__gthrw___pthread_key_create with gcc 4.8.4 and Eclipse Luna

Found out that it is somehow related to g++-4.8.

I installed g++-5 and the error was gone.

Here are the steps:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-5 libstdc++-5-dev

sudo rm -f /usr/bin/g++
sudo ln -s /usr/bin/g++-5 /usr/bin/g++

Boost Libraries Linking Issues

Did you remember to specify -pthread as well? From GCC manpage:

   -pthread
Add support for multithreading using the POSIX threads library.
This option sets flags for both the preprocessor and linker.

It is required with code that uses POSIX threads.



Related Topics



Leave a reply



Submit