首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CppuTest:在最终链接期间未定义对pthread的引用

CppuTest:在最终链接期间未定义对pthread的引用
EN

Stack Overflow用户
提问于 2015-08-04 16:43:22
回答 2查看 1.4K关注 0票数 0

我是cppuTest的新手,实际上我正在尝试在CppuTest根目录中构建./examples。源代码和测试文件的编译没有问题,但我在最后的链接阶段遇到了这个错误:

代码语言:javascript
复制
C:\CppUTest\cpputest-3.7.1\examples>make
compiling AllTests.cpp
compiling CircularBufferTest.cpp
compiling EventDispatcherTest.cpp
compiling HelloTest.cpp
compiling MockDocumentationTest.cpp
compiling PrinterTest.cpp
compiling CircularBuffer.cpp
compiling EventDispatcher.cpp
compiling Printer.cpp
compiling hello.c
Building archive lib/libCppUTestExamples.a
a - objs/ApplicationLib/CircularBuffer.o
a - objs/ApplicationLib/EventDispatcher.o
a - objs/ApplicationLib/Printer.o
a - objs/ApplicationLib/hello.o
Linking CppUTestExamples_tests
../lib/libCppUTest.a(UtestPlatform.cpp.obj): In function     `PThreadMutexCreate':
c:/CppUTest/cpputest-3.7.1/src/Platforms/Gcc/UtestPlatform.cpp:248:     undefined reference to `_imp__pthread_mutex_init'
../lib/libCppUTest.a(UtestPlatform.cpp.obj): In function `PThreadMutexLock':
c:/CppUTest/cpputest-3.7.1/src/Platforms/Gcc/UtestPlatform.cpp:255:     undefined reference to `_imp__pthread_mutex_lock'
../lib/libCppUTest.a(UtestPlatform.cpp.obj): In function     `PThreadMutexUnlock':
c:/CppUTest/cpputest-3.7.1/src/Platforms/Gcc/UtestPlatform.cpp:260:     undefined reference to `_imp__pthread_mutex_unlock'
../lib/libCppUTest.a(UtestPlatform.cpp.obj): In function     `PThreadMutexDestroy':
c:/CppUTest/cpputest-3.7.1/src/Platforms/Gcc/UtestPlatform.cpp:266:     undefined reference to `_imp__pthread_mutex_destroy'
collect2.exe: error: ld returned 1 exit status
make: *** [CppUTestExamples_tests] Error 1

我在Windows7上使用的是MinGW,MinGW也包含pthread.a库。我的makefil如下所示:

代码语言:javascript
复制
#---------
#
# CppUTest Examples Makefile
#
#----------

#Set this to @ to keep the makefile quiet
ifndef SILENCE
    SILENCE = @
endif

#--- Inputs ----#
COMPONENT_NAME = CppUTestExamples
CPPUTEST_HOME = ..

CPPUTEST_USE_EXTENSIONS = Y
CPP_PLATFORM = Gcc

CFLAGS = -Dmalloc=cpputest_malloc -Dfree=cpputest_free
CPPFLAGS =
GCOVFLAGS = -fprofile-arcs -ftest-coverage

LDFLAGS = -lpthread
#USER_LIBS = -lpthread

# This line is overriding the default new macros.  This is helpful
# when using std library includes like <list> and other containers
# so that memory leak detection does not conflict with stl.
CPPUTEST_MEMLEAK_DETECTOR_NEW_MACRO_FILE = -include     ApplicationLib/ExamplesNewOverrides.h
SRC_DIRS = \
    ApplicationLib

TEST_SRC_DIRS = \
    AllTests

INCLUDE_DIRS =\
  .\
  ApplicationLib\
  $(CPPUTEST_HOME)/include\

include $(CPPUTEST_HOME)/build/MakefileWorker.mk

如您所见,pthread lib被提供给带有LDFLAGS的链接器。

有类似经历的人吗?或者可能知道问题出在哪里?将会对任何提示表示感谢!

EN

回答 2

Stack Overflow用户

发布于 2015-08-05 15:28:42

感谢“基思·马歇尔”和“疯狂科学家”

所以不是LDFLAGS = -lpthread

我使用:

代码语言:javascript
复制
LD_LIBRARIES += -lpthread

并将此行直接放在前面:

代码语言:javascript
复制
include $(CPPUTEST_HOME)/build/MakefileWorker.mk

现在它起作用了。

票数 0
EN

Stack Overflow用户

发布于 2016-07-12 08:41:48

可以在Catalogue of Built-In Rules中看到

链接单个对象文件的

通过C编译器运行链接器(通常称为ld),从n.o自动生成n。使用的精确配方是:

$(CC) $(LDFLAGS) n.o $(LOADLIBES) $(LDLIBS)

Variables Used by Implicit Rules

低版本标志

当编译器应该调用链接器ld (如-L )时,要给它们的额外标志。应该将库(-lfoo)添加到LDLIBS变量中。

因此,在这种情况下,应该将-lpthread设置或添加到LDLIBS,而不是LDFLAGS。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31804587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档