【发布时间】:2015-10-26 13:56:42
【问题描述】:
我是 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.... 提供给链接器的。
有人有类似经历吗?或者也许知道问题出在哪里? 将感谢任何提示!
【问题讨论】:
-
您应该做的第一件事是取消设置“安静”模式,以便打印调用的命令行。然后检查用于链接的命令行并确保您希望看到的标志确实存在。您显示
LDFLAGS正在设置,但由于您没有显示链接规则,因此无法知道该变量是否在链接期间真正使用。此外,无论如何,在 UNIX 系统上,您需要在编译和链接期间使用-pthread标志。 -
除了@MadScientist 的评论之外,您还应该注意,如果
LDFLAGS被解释为在 GNU make 的默认规则中使用,那么它的评估太早而无法通过 任何 库有效地连接到链接器。您应该不在LDFLAGS中指定-lpthread;在链接器命令行上,-lpthread必须出现在 所有需要它的目标文件之后,但LDFLAGS可以在 之前 被解释。按照惯例,您可以将库添加到LIBS,not 到LDFLAGS,然后您应该确保$(LIBS)出现在链接器命令行上的所有目标文件之后 . -
关于
LDFLAGS的要点。但是,您想使用LDLIBS而不是LIBS...LIBS不是 GNU make 中的标准变量。 -
@MadScientist:是的,我可以在
make -p | grep LINK的输出中看到$(LDLIBS)的使用。然而,LIBS和LDLIBS都不是 GNU 编码标准强制要求的,但LIBS是自动工具项目的惯例,并在 GNU autoconf 手册中记录。
标签: makefile mingw undefined-reference cpputest pthreads-win32