【发布时间】: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