在Android NDK上使用Google Test测试'typeinfo for testing :: Test'的未定义引用

mxd*_*ois 5 c++ android makefile googletest 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    
Run Code Online (Sandbox Code Playgroud)

这是我目前所知道的:

  • ::testing::Test是由TEST()宏自动子类化的Google Test类.
  • 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
Run Code Online (Sandbox Code Playgroud)

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() );
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

小智 1

好吧,如果你想要建议……就让事情变得简单吧。如果您确实需要使用 google test 对 C++ 本机代码进行测试,请首先作为桌面版 google test 进行测试。

到目前为止,我可以看出,显然它还没有真正完全发挥作用 - 至少我没有找到任何有价值的材料或教程来表明它确实适用于任何现实世界的项目。

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

在这里您可以找到对此的进一步讨论:Unit Tests with NDK