未定义参考' typeinfo进行测试::测试'在Android NDK上使用Google Test

时间:2014-04-19 22:14:54

标签: android c++ android-ndk makefile googletest

我正在尝试将Android测试与Android NDK一起使用。在NDK README example here之后,我已经设置了我的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错误。
  • could be caused by compiling google test with different flags,但不应该在这里,因为我使用的是静态Google测试库,两个模块之间的标志相同。

我错过了什么?或者我在哪里可以看到下一个?谢谢!

更新:如果我从ndkfoo_unittest模块中删除SHARED_LIBRARIES,CPP_FLAGS和LDLIBS,我可以构建一个不依赖于Boost或Android原生apis的简单Google测试。

构建命令:

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)

3 个答案:

答案 0 :(得分:2)

我相信这是因为您没有启用RTTI。尝试添加

APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti

到Application.mk。这解决了我。根据文档,您应该能够指定

LOCAL_CPP_FEATURES := rtti exceptions
在Android.mk中

,但这对我使用NDK r10c不起作用。另请注意,启用RTTI并非强制启用异常,但使用RTTI的代码必须链接到支持RTTI的标准库,而带RTTI的库也支持异常。

答案 1 :(得分:1)

好吧,如果你想要一个建议...只是保持简单。如果您确实需要使用谷歌测试对C ++本机代码进行测试,请首先将其作为Google桌面测试。

到目前为止,我可以说,显然它还没有真正完整的功能 - 至少我没有找到任何有价值的材料或教程,表明它真的适用于任何现实世界的项目。

您现在可以做的是:使用JUnit和JNI来测试嵌入在设备中的C ++。这就是方法。我希望将来谷歌测试能真正适用于Android NDK。

在这里您可以找到关于它的进一步讨论:Unit Tests with NDK

答案 2 :(得分:0)

我遇到了同样的问题。吉姆是正确的,这是因为RTTI。为了使其工作,您还需要在启用RTTI的情况下构建libgtest.a。你可以补充一下 LOCAL_CFLAGS:= -fexceptions -frtti在Android.MK中用于gtest并重建.a文件。然后使用新的.a文件来构建项目。我试过了,它对我来说很好。:)