简体   繁体   English

Android NDK错误:未定义参考

[英]Android NDK error: undefined reference

There have been some questions similar to mine, but it seems that their solutions don't work for me. 曾经有一些类似于我的问题,但是似乎他们的解决方案对我不起作用。

I'm trying to compile dumpsys source code using Android NDK. 我正在尝试使用Android NDK编译dumpsys源代码。 I have added a couple of lines to Android.mk to include the libraries. 我在Android.mk中添加了几行以包含这些库。 The final Android.mk file looks like this: 最终的Android.mk文件如下所示:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
        dumpsys.cpp

LOCAL_SHARED_LIBRARIES := \
        libutils \
        liblog \
        libbinders

ANDROID_SRC="my android source directory"
LOCAL_C_INCLUDES := ${ANDROID_SRC}/frameworks/native/include \
                        ${ANDROID_SRC}/system/core/include

#$(warning $(TARGET_C_INCLUDES))

LOCAL_MODULE:= dumpsys

TARGET_ARCH := arm

TARGET_ARCH_ABI := armeabi-v7a

include $(BUILD_EXECUTABLE)

when I execute ndk-build, I get the following errors: 执行ndk-build时,出现以下错误:

/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:144: error: undefined reference to 'android::String16::~String16()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:240: error: undefined reference to 'android::VectorImpl::finish_vector()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:241: error: undefined reference to 'android::VectorImpl::~VectorImpl()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:135: error: undefined reference to 'android::String16::String16()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:154: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:166: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/String16.h:178: error: undefined reference to 'strzcmp16'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:32: error: undefined reference to 'android::defaultServiceManager()'
jni/dumpsys.cpp:35: error: undefined reference to '__android_log_print'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:245: error: undefined reference to 'android::VectorImpl::operator=(android::VectorImpl const&)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:378: error: undefined reference to 'android::VectorImpl::sort(int (*)(void const*, void const*))'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:66: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:81: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:89: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aerr'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aout'
collect2: error: ld returned 1 exit status

How should I solve this issue? 我应该如何解决这个问题?

Thanks in advance. 提前致谢。

I'm working on a similar issue here. 我正在处理类似的问题。

Basically, dumpsys is an AOSP component and intended to be built with the AOSP toolchain. 基本上,dumpsys是AOSP组件,旨在与AOSP工具链一起构建。 You will need to apply some tweaks to port it to NDK – including stuff from ${ANDROID_SRC} is the first step but not the whole story. 您将需要进行一些调整,以将其移植到NDK –包括${ANDROID_SRC}是第一步,但不是全部。

You've successfully included the headers and thus made the compiler happy. 您已经成功包含了标头,从而使编译器满意。 Now the linker is complaining because it cannot find the libraries you are linking against. 现在,链接器在抱怨,因为它找不到您要链接的库。 The good news is that they are shared libraries, thus having the libraries around at build time isn't a strict requirement. 好消息是它们是共享库,因此在构建时不存在严格的要求。

The NDK defines a stable API of libraries you can use, which is documented here . NDK的定义库的一个稳定的API,你可以使用,这是记录在这里 liblog is in that list and can be included by adding the following lines to Android.mk : liblog在该列表中,可以通过在Android.mk添加以下几行来包括它:

LOCAL_LDLIBS := \
  -llog \

The other two libraries are not part of the stable API. 另外两个库不是稳定API的一部分。 This essentially means that even if your code works on a particular version of Android, it may break on any later version because the API may have changed – you might want to bear this in mind. 从本质上讲,这意味着即使您的代码在特定版本的Android上运行,也可能会在任何更高版本上中断,因为API可能已更改–您可能要牢记这一点。

Since these libraries are shared, all ld does is check if they actually provide the functions you're using. 由于这些库是共享的,因此ld所做的所有工作就是检查它们是否确实提供了您正在使用的功能。 This question and its accepted answer have instructions for getting rid of the related error messages: 该问题及其公认的答案提供了摆脱相关错误消息的说明:

One way is to use something like: 一种方法是使用类似:

LOCAL_LDFLAGS := -Wl,--unresolved-symbols=ignore-all

However, this will bypass all checks – so if you try to use a function that is indeed absent from the library, ld has no chance of warning you. 但是,这将绕过所有检查–因此,如果您尝试使用库中确实不存在的函数,则ld没有机会警告您。

The cleaner, but more work-intensive approach, is to provide stub libraries. 较干净,但工作量较大的方法是提供存根库。 A stub library is essentially a dummy library which defines the same symbols (functions etc.) as the "real" thing but has no implementation (functions simply return without doing anything). 存根库本质上是一个虚拟库,它定义与“真实”事物相同的符号(函数等),但是没有实现(函数只是不做任何事情就返回)。 It's enough to make the linker happy, but the libraries are not shipped and their "real" counterparts are used at runtime. 使链接器满意就足够了,但是没有提供这些库,并且在运行时使用它们的“真实”对应物。

You would need to get the source code for the two libraries, which reside in the following directories: libutils and libbinders 您将需要获取这两个库的源代码,它们位于以下目录中: libutilslibbinders

system/core/libutils
frameworks/native/libs/binder

Copy these two dirs into your project's jni dir. 将这两个目录复制到项目的jni目录中。 Then strip the code down: * Edit Android.mk , removing all build targets other than BUILD_SHARED_LIBRARY . 然后剥离代码:*编辑Android.mk ,除去BUILD_SHARED_LIBRARY以外的所有构建目标。 * Edit the source code files, replacing all function bodies with a simple return . *编辑源代码文件,用简单的return替换所有功能体。 It doesn't matter what you return, as long as you get the code to compile. 只要您可以编译代码,返回什么都无所谓。

Eventually you will probably need to prevent the stub libraries from getting included in your .apk (I have yet to figure out how to do that). 最终,您可能需要阻止存根库包含在您的.apk (我还没有弄清楚该怎么做)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM