Search code examples
android-ndklinkerstatic-librariesandroid-sourcestatic-linking

Unable to link prebuilt static library with shared library in Android NDK


AOSP 7.1.1_r50 Tree

├── abi
├── art
├── bionic
├── bootable
├── build
├── cts
├── dalvik
├── developers
├── development
├── device
├── docs
├── external
├── frameworks
├── hardware
├── libcore
├── libnativehelper
├── libtest
│   ├── libA
│   └── libB
│   └── libC
│   └── libmytest
│   └── └── Android.mk
│   └── └── my_test.c
│   └── └── curve_test.c
│   └── └── LibSecureTest_Wrapper.cpp
│   └── libsecuretest
│   └── └── arm64-v8a
│   └── └── └── libsecuretest.a
│   └── └── armeabi-v7a
│   └── └── └── libsecuretest.a
│   └── └── Android.mk
├── ndk
├── out
├── packages
├── pdk
├── platform_testing
├── prebuilts
├── sdk
├── system
├── toolchain
└── tools

I'm trying to build libmytest as shared library. libmytest need to be linked with libsecuretest.a which is a prebuilt static library. make libsecuretest command successfully builds but when I execute make libmytest command gives linker command failed error. You can find the complete output log here.

PS: libsecuretest.a is C++ library built with NDK.

Here is the content of Android.mk file located in libmytest directory:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libmytest

LOCAL_SRC_FILES := \
curve_test.c \
my_test.c \
LibSecureTest_Wrapper.cpp

LOCAL_C_INCLUDES := \
$(LOCAL_PATH) \
$(LOCAL_PATH)/../include \
$(LOCAL_PATH)/../../emulator/include

LOCAL_CFLAGS := -DANDROID -g -DTA_PLUGIN

LOCAL_CPPFLAGS := -std=c++11
LOCAL_CPP_EXTENSION := .cpp
LOCAL_NDK_STL_VARIANT := c++_static

LOCAL_SHARED_LIBRARIES := libc libstdc++ libA libB libC
LOCAL_STATIC_LIBRARIES := libsecuretest

ifeq ($(TARGET_ARCH),arm)
LOCAL_LDFLAGS := -Wl,--hash-style=sysv
endif

include $(BUILD_SHARED_LIBRARY)

And here is the content of Android.mk file located in libsecuretest directory:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libsecuretest
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libsecuretest.a
LOCAL_MODULE_SUFFIX := .a
LOCAL_MODULE_TAGS := optional
include $(BUILD_PREBUILT)

I can't find out what exactly I missed. Any help will be very much appreciated.


Solution

  • Your problem is probably because of you mix different STL's (versions)

    Most of your "undefined reference" is related to:

    std::__1
    std::__ndk1
    

    Like:

    out/target/product/generic_arm64/obj/STATIC_LIBRARIES/libsecuretest_intermediates/libsecuretest.a(MyUtils.cpp.o):function MyUtils::Int32toHexString(int, int): error: undefined reference to 'std::__ndk1::locale::locale()'
    

    Please try to compile also the "libsecuretest" with the AOSP build (maybe the libc++ versions is different).

    NOTE:

    I tried to check my AOSP libc++ symbols (PLATFORM_VERSION=6.0.1) and get:

    $ nm -DC libc++.so | grep 'locale::locale()'
    000682ad T std::__1::locale::locale()
    000682ad T std::__1::locale::locale()
    

    Also I get same symbols with libc++ from the phones Pixel2(Android9) S8(Andorid 8.0.0)

    adb pull /system/lib/libc++.so
    

    [And check it with the same "nm" command...]

    But the symbols of the ndk18 is:

    $nm -DC libc++_shared.so | grep 'locale::locale()'
    00000000000865b0 T std::__ndk1::locale::locale()
    00000000000865b0 T std::__ndk1::locale::locale()