0

我是 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 库被提供给带有 LDFLAGS 的链接器......

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

4

2 回答 2

0

感谢@Keith Marshall 和@MadScientist,

所以代替 LDFLAGS = -lpthread

我用了:

LD_LIBRARIES += -lpthread

并将这一行直接放在前面:

include $(CPPUTEST_HOME)/build/MakefileWorker.mk

现在它起作用了。

于 2015-08-05T07:28:42.367 回答
0

从内置规则目录中可以看出:

链接单个目标文件

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

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

隐式规则使用的变量

LDFLAGS

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

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

于 2016-07-12T00:41:47.940 回答