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

时间:2015-08-04 08:43:22

标签: makefile mingw undefined-reference cpputest pthreads-win32

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

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

我在Windows 7上使用MinGW.MinGW还包含pthread.a库。我的makefil看起来如下:

#---------
#
# 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 ....给链接器的。

有人有类似的经历吗?或者也许知道问题出在哪里? 将感谢任何提示!

2 个答案:

答案 0 :(得分:0)

thx to @Keith Marshall和@MadScientist,

所以代替     LDFLAGS = -lpthread

我用过:

LD_LIBRARIES += -lpthread

并将此行直接放在:

之前
include $(CPPUTEST_HOME)/build/MakefileWorker.mk

现在可行。

答案 1 :(得分:0)

Catalogue of Built-In Rules

中可以看出
  

链接单个目标文件

     

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

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

Variables Used by Implicit Rules

  

LDFLAGS

     

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

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