0

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.

Community
  • 1
  • 1
john
  • 97
  • 1
  • 8
  • 3
    Have you tried using `-pthread` as indicated in your first linked question? – martin May 07 '15 at 06:41
  • +2 the above or try moving -lphtread to the beginning of the link flags – Stephan Dollberg May 07 '15 at 06:51
  • I tried `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 -pthread -o $@ ` but still didn't work, same as through moving it to the beginning of the link flags – john May 07 '15 at 08:19

1 Answers1

1

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

zee
  • 11
  • 1