链接预构建的静态库ndk与Android源代码

时间:2016-09-28 19:59:03

标签: android android-ndk linker static-libraries

我最近发现,从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;
    }

Android mk:

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树中链接静态库的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

  

为什么会这样?

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

  

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

在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文件中定义。