gpt4 book ai didi

android - 链接预建静态库 ndk 与 Android 源代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:13 24 4
gpt4 key购买 nike

我最近发现,从 ndk-build 链接预构建的静态库与从 android 源代码树 (mm) 中链接有根本的不同。这是为什么?

   // main.cpp
#include <stdio.h>
#include "doubler.hpp"

int main()
{
printf("test a static lib \n");

// library function
doubler *p = new doubler();
delete p;

return 0;
}

安卓手机:

LOCAL_PATH := $(call my-dir)

###################### static lib ##################
### prebuilt lib works in NDK but not Android src
#include $(CLEAR_VARS)
#LOCAL_MODULE := doubleIt_prebuilt
#LOCAL_SRC_FILES := $(LOCAL_PATH)/libDoubler.a
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
##LOCAL_SRC_FILES := libDoubler.a
#include $(PREBUILT_STATIC_LIBRARY)

###################### test app ##################
include $(CLEAR_VARS)

# binary name
LOCAL_MODULE:= testApp

# c++ file extension
LOCAL_CPP_EXTENSION := .cpp

# src files
LOCAL_SRC_FILES := main.cpp

# include dir
LOCAL_C_INCLUDES := $(LOCAL_PATH)

### this works in Android src, but not in NDK
LOCAL_LDLIBS := -L$(LOCAL_PATH) -lDoubler

### prebuilt lib works in NDK but not Android src
#LOCAL_STATIC_LIBRARIES := doubleIt_prebuilt

#what to build
include $(BUILD_EXECUTABLE)

最终,在 src 树中链接静态库的正确方法是什么?

最佳答案

Why is this?

ndk-build 和 AOSP 构建系统是两个完全不同的构建系统,不幸的是它们看起来非常相似。

Ultimately, what is the correct way to link a static lib in the src tree?

在 AOSP 树中?预建模块的定义不同。这里有几个例子:

https://android.googlesource.com/platform/prebuilts/sdk/+/9c011b3a7784803b96dc0f0a840aa9033a0cd62a/tools/Android.mk#291

include $(CLEAR_VARS)
LOCAL_MODULE := libbcc
LOCAL_SRC_FILES := $(HOST_OS)/lib64/$(LOCAL_MODULE)$(HOST_SHLIB_SUFFIX)
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := $(HOST_SHLIB_SUFFIX)
LOCAL_IS_HOST_MODULE := true
LOCAL_MULTILIB := 64
include $(BUILD_PREBUILT)

https://android.googlesource.com/platform/development/+/518e6c3a28cc63fd094c8b255e268650b03fdab5/host/windows/prebuilt/usb/Android.mk

include $(CLEAR_VARS)
LOCAL_IS_HOST_MODULE := true
LOCAL_MODULE := AdbWinApi
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_SRC_FILES_x86 := AdbWinApi.a
LOCAL_MODULE_SUFFIX := .a
LOCAL_MULTILIB := 32
LOCAL_MODULE_HOST_OS := windows
include $(BUILD_PREBUILT)

然后您可以像使用任何其他库一样使用它们:LOCAL_STATIC_LIBRARIES := libmyprebuilt。以上两个示例都是针对主机模块的。对于目标模块,只需删除该行即可。

请注意,AOSP 的新(仍在开发中)构建系统 Soong 尚不支持预构建模块。这些现在只能在 Android.mk 文件中定义。

关于android - 链接预建静态库 ndk 与 Android 源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39756620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com