gpt4 book ai didi

c++ - 未定义对 Gtest 的 pthread 的引用

转载 作者:行者123 更新时间:2023-11-28 06:16:56 27 4
gpt4 key购买 nike

从昨天开始,我一直在摸不着头脑,试图让 gtest 正常工作,但在阅读下面的链接后我就是无法修复它。

undefined reference to `pthread_key_create' (linker error)

error during making GTest

显示的编译错误是这样的:

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

我的 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 $@

我还在学习Linux、编译和链接源文件的过程中。

最佳答案

错误是关于 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链接有一个很好的 Makefile,它可以解决您的所有问题,并且它遵循创建 Makefile 的一般标准

关于c++ - 未定义对 Gtest 的 pthread 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com