[英]undefined reference to 'typeinfo for testing::Test' with Google Test on Android NDK
我正在尝试使用Android NDK进行Google测试。 在这里的NDK README示例之后,我已经设置了我的Android.mk和单个测试,如下所示,但我收到此错误:
./obj/local/armeabi/objs-debug/ndkfoo_unittest/FilteredPriorityQueue_test.o:FilteredPriorityQueue_test.cpp:function typeinfo for mashbot::FilteredPriorityQueueTest_ShouldRetrieveTop_Test: error: undefined reference to 'typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi/ndkfoo_unittest] Error 1
这是我目前所知道的:
::testing::Test
是由TEST()
宏自动子类化的Google Test类。 undefined reference to 'typeinfo for
errors的undefined reference to 'typeinfo for
。 我错过了什么? 或者我在哪里可以看到下一个? 谢谢!
更新:如果我从ndkfoo_unittest模块中删除SHARED_LIBRARIES,CPP_FLAGS和LDLIBS,我可以构建一个不依赖于Boost或Android原生apis的简单Google Test。
构建命令:
ndk-build SHELL=/bin/bash NDK_DEBUG=1
FilteredPriorityQueue_test.cpp:
#include "gtest/gtest.h"
// FilteredPriorityQueue is a header-only file with no virtual methods.
#include "FilteredPriorityQueue.h"
// So is Comparator.
#include "Comparator.h"
struct MaskedObject {
int mMask;
MaskedObject(int mask) : mMask(mask) { }
int getMask() const { return mMask; }
bool operator<(const MaskedObject& rhs) const {
return this->mMask < rhs.mMask;
}
};
typedef
FilteredPriorityQueue<int, MaskedObject, Comparator<MaskedObject> > TestQueue;
TEST(FilteredPriorityQueueTest,ShouldRetrieveTop) {
Comparator<MaskedObject> comparator(Comparator<MaskedObject>::LESS);
TestQueue q(comparator);
q.push(1, MaskedObject(1));
q.push(2, MaskedObject(2));
q.push(4, MaskedObject(4));
EXPECT_EQ( 1, q.top().getMask() );
}
Android.mk:
# ndkfoo module
#-------------------------
LOCAL_MODULE := ndkfoo
LOCAL_CPPFLAGS := -frtti -pthread -fexceptions -std=c++11
LOCAL_LDLIBS += -lOpenSLES -llog -landroid
LOCAL_C_INCLUDES += $(LIBMASHBOT_ROOT)/include
LOCAL_C_INCLUDES += $(BOOST_INCLUDE_PARENT)
LOCAL_SHARED_LIBRARIES += mashbot \
gnustl_shared \
boost_thread-gcc-mt-1_53 \
boost_system-gcc-mt-1_53 \
$(BOOST_LIB)
LOCAL_SRC_FILES := ndkfoo.cpp \
#...more files...
include $(BUILD_SHARED_LIBRARY)
# ndkfoo tests module
#-------------------------
include $(CLEAR_VARS)
LOCAL_MODULE := ndkfoo_unittest
LOCAL_CPPFLAGS := -frtti -pthread -fexceptions -std=c++11
LOCAL_C_INCLUDES += $(BOOST_INCLUDE_PARENT)
LOCAL_STATIC_LIBRARIES := googletest_main
LOCAL_SHARED_LIBRARIES += ndkfoo \
gnustl_shared \
$(BOOST_LIB)
LOCAL_SRC_FILES := FilteredPriorityQueue_test.cpp
include $(BUILD_EXECUTABLE)
# this imports $NDK/sources/third_party/googletest/Android.mk
$(call import-module,third_party/googletest)
我相信这是因为你没有启用RTTI。 尝试添加
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti
到Application.mk。 这解决了我。 根据文档,您应该能够指定
LOCAL_CPP_FEATURES := rtti exceptions
在Android.mk,但这对我使用NDK r10c不起作用。 另请注意,启用RTTI不是必需的,但使用RTTI的代码必须链接到支持RTTI的标准库,而带RTTI的库也支持异常。
好吧,如果你想要一个建议...只是保持简单。 如果您确实需要使用谷歌测试对C ++本机代码进行测试,请首先将其作为Google桌面测试。
到目前为止,我可以说,显然它还没有真正完整的功能 - 至少我没有找到任何值得的材料或教程,表明它真的适用于任何现实世界的项目。
您现在可以做的是:使用JUnit和JNI来测试嵌入在设备中的C ++。 这就是方法。 我希望将来谷歌测试真正适用于Android NDK。
在这里您可以找到关于它的进一步讨论: 使用NDK进行单元测试 。
我遇到了同样的问题。吉姆是正确的,这是因为RTTI。 为了使其工作,您还需要在启用RTTI的情况下构建libgtest.a。 你可以在Android.MK中为gtest添加LOCAL_CFLAGS:= - fexceptions -frtti并重建.a文件。 然后使用新的.a文件来构建项目。 我试过,它对我来说很好。:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.