【问题标题】:undefined reference to 'typeinfo for testing::Test' with Google Test on Android NDK在 Android NDK 上使用 Google Test 对“typeinfo for testing::Test”的未定义引用
【发布时间】:2014-06-04 06:23:17
【问题描述】:

我正在尝试将 Google Test 与 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 原生 api 之类的简单 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)

【问题讨论】:

  • 您需要向我们展示导致错误的ndkfoo_unittest 的链接器命令行。
  • @MikeKinghan 当然可以。我将它添加到问题中。我有像BOOST_INCLUDE_PARENT 这样的环境变量。这些工作正常。
  • 那不是链接器命令,那是ndk-build 命令。在构建编译您的源文件后,它会调用链接器 (ld) 来创建您的可执行文件。我们需要它生成的ld 命令行。您向我们展示了一个链接错误,但没有向我们展示正在链接的内容。

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


【解决方案1】:

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

APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti

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

LOCAL_CPP_FEATURES := rtti exceptions

在 Android.mk 中,但这对我使用 NDK r10c 不起作用。另请注意,启用 RTTI 并不是必须启用异常,但使用 RTTI 的代码必须链接到支持 RTTI 的标准库,并且具有 RTTI 的库也支持异常。

【讨论】:

    【解决方案2】:

    好吧,如果您需要建议...请保持简单。如果您确实需要使用 google test 对 C++ 本机代码进行测试,请先作为 google test for desktop 进行测试。

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-26
        • 1970-01-01
        • 2019-03-11
        • 2021-02-16
        • 2010-09-23
        • 2012-07-18
        • 2019-03-08
        相关资源
        最近更新 更多