未定义的'testing :: UnitTest :: GetInstance()“引用

时间:2016-10-11 12:03:16

标签: c++ makefile g++ googletest

我正在从Eclipse自动创建的makefile迁移到手动makefile。它确实适用于Eclipse,但不适用于我的。这是编译器输出:

13:54:30 **** Incremental Build of configuration Debug for project MY_PROJECT ****
make -f ../build/makefile all 
Building file: ../src/TestLauncher.cpp
g++ -std=c++11 -I"../src" -I"../src/Common" -I"../src/Common/COMPUTATION" -I"../src/Common/DATA_ACQUISITION" -I"/usr/include/mysql" -I"/usr/include/gtest" -O0 -g3 -pg -Wall -Wextra -fmessage-length=0 --coverage -fPIC  -MMD -MP -MF"../bin/objs/TestLauncher.d" -MT"../bin/objs/TestLauncher.o" -o "../bin/objs/TestLauncher.o" "../src/TestLauncher.cpp"
/tmp/ccrDQ0Bm.o: In function `main':
/home/user/workspace/MY_PROJECT/build/../src/TestLauncher.cpp:13: undefined reference to `testing::InitGoogleTest(int*, char**)'
../build/makefile:165: recipe for target '../bin/objs/TestLauncher.o' failed
/tmp/ccrDQ0Bm.o: In function `RUN_ALL_TESTS()':
/usr/include/gtest/gtest.h:2288: undefined reference to `testing::UnitTest::GetInstance()'
/usr/include/gtest/gtest.h:2288: undefined reference to `testing::UnitTest::Run()'
collect2: error: ld returned 1 exit status
make: *** [../bin/objs/TestLauncher.o] Error 1

13:54:31 Build Finished (took 989ms)

我已经找到了这个错误,它似乎总是与链接器标志之间没有使用-lgtest有关。我是,但无论如何,这个错误不会发生在链接器上,而是在编译阶段。因此,我不明白为什么会出现这个问题。

我还编译了gtest库,它在/usr/lib/中:正如我所说,它适用于Eclipse的makefile。

这是我的makefile:

-include ../makefile.init

RM := rm -rf

EXECUTABLE_NAME := MY_PROJECT

FLAGS := -O0 -g3 -pg -Wall -Wextra -fmessage-length=0 --coverage -fPIC #-v

C++_DEPS :=
C_DEPS := 
CC_DEPS := 
CPP_DEPS := 
CXX_DEPS := 
C_UPPER_DEPS := 

EXECUTABLES := ../bin/$(EXECUTABLE_NAME)

LIBS_SERVER := -ldl -lpthread -lgtest -lboost_date_time -lmysqlclient -lboost_filesystem -lboost_system
INCLUDES_LIB_SERVER := -L /usr/lib/mysql -L /usr/lib/boost


CPP_SRCS += \
../src/TestLauncher.cpp \
../src/Common/DATA_ACQUISITION/DATA_ACQUISITION_test.cpp

OBJS += \
../bin/objs/TestLauncher.o \
../bin/objs/DATA_ACQUISITION_test.o

CPP_DEPS += \
../bin/objs/TestLauncher.d \
../bin/objs/DATA_ACQUISITION_test.d


INCLUDES_SERVER += \
-I"../src" \
-I"../src/Common" \
-I"../src/Common/COMPUTATION" \
-I"../src/Common/DATA_ACQUISITION" \
-I"/usr/include/mysql" \
-I"/usr/include/gtest"


ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
endif

-include ../makefile.defs


###############################################################################
# TARGETS
###############################################################################


# All Target
all: $(EXECUTABLES)

test: FLAG=-DTEST
test: all

../bin/$(EXECUTABLE_NAME): $(OBJS)
    @echo 'Building target: $@'
    @echo 'Invoking: Cygwin C++ Linker'
    g++ $(FLAGS) -o"../bin/$(EXECUTABLE_NAME)" $(OBJS) $(INCLUDES_LIB_SERVER) $(LIBS_SERVER)  
    @echo 'Finished building target: $@'
    @echo ' '


# Other Targets
clean:
    -$(RM) $(OBJS) $(OBJS_RT) $(C++_DEPS) $(EXECUTABLES)$(CC_DEPS)$(C_DEPS)$(CPP_DEPS)$(CXX_DEPS)$(C_UPPER_DEPS) ../bin/MY_PROJECT
    -@echo ' '

.PHONY: all clean dependents
.SECONDARY:

###############################################################################
# COMPILE --> OBJS
###############################################################################

../bin/objs/%.o: ../src/%.cpp
    @echo 'Building file: $<'
    g++ -std=c++11 $(INCLUDES_SERVER) $(FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '


../bin/objs/%.o: ../src/Common/%.cpp 
    @echo 'Building file: $<'
    g++ -std=c++11 $(INCLUDES_SERVER) $(FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '


../bin/objs/%.o: ../src/Common/COMPUTATION/%.cpp
    @echo 'Building file: $<'
    g++ -std=c++11 $(INCLUDES_SERVER) $(FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '


../bin/objs/%.o: ../src/Common/DATA_ACQUISITION/%.cpp
    @echo 'Building file: $<'
    g++ -std=c++11 $(INCLUDES_SERVER) $(FLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -o "$@" "$<"
    @echo 'Finished building: $<'
    @echo ' '

知道什么是错的?

1 个答案:

答案 0 :(得分:0)

请记住,对于-l标志,顺序非常重要。你确定-lgtest处于正确的位置吗?

此外,链接器需要一个用于-lgtest的libgtest.so文件,而不仅仅是gtest。

希望它有所帮助。