对Gtest的未定义的pthread引用

时间:2021-05-26 05:28:51

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.

从昨天开始尝试做gtest工作以来,我一直在摸不着头脑,但是在阅读下面的链接后我无法修复它。

undefined reference to `pthread_key_create' (linker error)

对'pthread_key_create'的未定义引用(链接器错误)

error during making GTest

制作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:

我的Makefile是:

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.

我仍然在学习Linux,编译和链接源文件。

1 个解决方案

#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.

错误是关于pthread上的链接错误。您必须将pthread标志设置为-pthread,并且后续CXX应该可以解决问题。

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

这个链接有一个很好的Makefile,可以解决你所有的问题,它遵循创建Makefile的一般标准

#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.

错误是关于pthread上的链接错误。您必须将pthread标志设置为-pthread,并且后续CXX应该可以解决问题。

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

这个链接有一个很好的Makefile,可以解决你所有的问题,它遵循创建Makefile的一般标准