undefined reference to `pthread_key_create' (linker error)

我的未来我决定 提交于 2019-11-27 02:09:10

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

You need to specify -pthread after -lgtest. The linker takes libraries in order, and only takes as much as it needs to resolve references which are undefined at that point.

jimi

Nope, the problem is with Gtest's build.

If you build it using the standard configure approach, it isn't supplying the -lpthread correctly to create libgtest.so. Hence, when you try building a final shared library that actually uses the pthread capability it fails.

Instead, use the Cmake approach:

cd gtest-1.7.0
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON ..
make 

And then manually install these into /usr/lib/

This version correctly links in libpthread into libgtest.

Use -pthread instead of -lpthread (for linking with pthread-library), while you using gtest in your executable.

OR

Move the -lpthread after libgtest.a (sequence matters).

To answer we probably need more information, are you on a 64 bit machine and downloaded a 32 bit library?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!