简体   繁体   中英

Undefined reference to pthread for Gtest

I have been scratching my head since yesterday trying to make gtest work but I just can't fix it after reading the links below.

undefined reference to `pthread_key_create' (linker error)

error during making GTest

The compilation error displayed is this:

g++ main.o tests.o var.o -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs 
-lgtest -lgmock -lpthread -o test 
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
../gmock/gtest/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1

My Makefile is:

CXXFLAGS=-I ../gmock/include -I ../gmock/gtest/include
test:main.o tests.o var.o 
    g++ $^ -L ../gmock/lib/.libs -L ../gmock/gtest/lib/.libs -lgtest -lgmock -lpthread -o $@ 

I am still in the process of learning Linux, compiling and linking source files.

The Error is about linking error on pthread. You have to make the pthread flag to -pthread and the follow CXX should do the trick.

LDLIBS =  -L../gmock/lib/.libs -L../gmock/gtest/lib/.libs -lgtest -lgmock 
test:main.o tests.o var.o
    g++ -isystem $(LDLIBS) -pthread  $^ -o $@

This link have a good Makefile which addresses all your problems and it follows general standards on creating a Makefile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM