36

我试图在我的 Ubuntu 机器上设置 GTest 环境。但是在使 GTest 获取库时,我收到以下错误...

som@som-VPCEH25EN:~/Workspace/CPP/gtest-1.6.0/make$ make
g++ -I../include -g -Wall -Wextra -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
gtest_main.a(gtest-all.o): In function `~ThreadLocal':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1336: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1340: undefined reference to `pthread_key_delete'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1336: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1340: undefined reference to `pthread_key_delete'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::GetOrCreateValue() const':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1372: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1379: undefined reference to `pthread_setspecific'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1365: undefined reference to `pthread_key_create'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::CreateKey()':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1365: undefined reference to `pthread_key_create'
gtest_main.a(gtest-all.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue() const':
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1372: undefined reference to `pthread_getspecific'
/home/som/Workspace/CPP/gtest-1.6.0/make/../include/gtest/internal/gtest-port.h:1379: undefined reference to `pthread_setspecific'
collect2: ld returned 1 exit status
make: *** [sample1_unittest] Error 1
4

5 回答 5

54

尝试在您的 g++ 命令中-lpthread移至 after 。gtest_main.a

于 2012-04-25T17:11:54.083 回答
17

我遇到了同样的问题,我-lpthread将 Makefile 更改为-pthread. (我去掉了小“L”。)之后,一个“make”编译没有错误。

于 2012-12-11T22:03:48.737 回答
6

您是否看过Erik Smistad关于如何在 Ubuntu 上开始使用gtest 的优秀教程?我刚刚在 (K)ubuntu 12.04 上尝试过,效果很好。简而言之,您需要做的是

sudo apt-get install cmake libgtest-dev
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib

如果您对以 root 身份运行 make 和 cmake 感到不舒服,则必须将 /usr/src/gtest 复制到用户可写的目录中。在本教程中,Erik 还提供了一个带有 CMakeLists.txt 的示例测试来构建它。

于 2012-07-17T10:14:52.907 回答
3

将“-L/usr/local/$(uname -m)-linux-gnu”放在 LDFLAGS 中,将“-pthread”放在链接器 (ld) 命令行中。(是的,那是-pthread,不是-lpthread)

于 2012-05-18T22:54:54.057 回答
0

gtest-1.6.0/make 中的文件 你应该有 Makefile 看看下面它应该是什么样子。区别在最后一行

# A sample Makefile for building Google Test and using it in user
# tests.  Please tweak it to suit your environment and project.  You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ..

# Where to find user code.
USER_DIR = ../samples

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = sample1_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.

all : $(TESTS)

clean :
    rm -f $(TESTS) gtest.a gtest_main.a *.o

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc

sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc

sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS)  $^ -o $@ -lpthread #<----------------------- DIFF

接下来请在此目录中运行 make :) 并感到高兴。

于 2013-04-26T08:09:34.573 回答