简体   繁体   中英

using pre-built static libraries for Android NDK development

I am trying to build an android application that uses static libraries from some existing c++ code. However I cannot seem to get things building, here are the steps I have taken so far..

I have ndk-r5b and have built the standalone toolchain as per ndk/docs/STANDALINE-TOOLCHAIN.html. I have then used the standalone toolchain compiler (arm-linux-androideabi-g++) instead of g++ for the CXX flag in the Makefile that compiles the static libraries I need. This compiles without errors and there are 3 static libraries produced. Here is a code snippet of some of the flags used to build the prebuilt libraries:

CXX = arm-linux-androideabi-g++
SYSTEM_LIBS = -lstdc++ -lm
INCLUDE_PATH += ${NDK_PATH}/platforms/android-8/arch-arm/usr/include/

Here is a sample line that is produced from the makefile when compiling:

arm-linux-androideabi-g++ -c -DTIME_SIM -I./include  -I/home/greg/dev/Android/android-ndk-r5b/platforms/android-8/arch-arm/usr/include/ -fpic -ggdb3 -SimTime.C -o SimTime.o

Next I build the app using ndk-build using the following for Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := engine
LOCAL_SRC_FILES := ../libs/libEngine.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := shmem
LOCAL_SRC_FILES := ../libs/libShMem.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := util
LOCAL_SRC_FILES := ../libs/libUtil.a
include $(PREBUILT_STATIC_LIBRARY)

# build server as a shared library
include $(CLEAR_VARS)
LOCAL_MODULE := libServer   
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../include 

LOCAL_SRC_FILES := \
    Server.C \
    Router.C \
    RouterMsgs.C \
    Federation.C \
    cripName.C \
    ver.C \
    JNIWrapper.cpp
LOCAL_STATIC_LIBRARIES := engine shmem util
include $(BUILD_SHARED_LIBRARY)

The prebuilt libraries compile fine using the standalone toolchain compiler given in the android ndk. However there are many unresolved references to ostream when linking the shared library to the prebuilt libraries using ndk-build. For exampe:

/home/android/obj/local/armeabi/libShMem.a(SubscriptionItem.o): In function `SUBSCRIPTION_ITEM::Print(std::basic_ostream<char, std::char_traits<char> >&)':/home/src/comm/SubscriptionItem.C:97: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

I assume I am missing some important flags or not doing something correct when I am compiling using the standalone compiler but any help or insight on this issue would be greatly appreciated as I cant seem to find this answer on google or in any of the android ndk docs. Thanks!

Well, you can actually fix that with creating a Application.mk file inside the same folder as the Android.mk file is, containing:

APP_STL := stlport_static

for using the static stlport that is located inside the Android NDK.

I had the same issue and resolved it by adding a module for the standard C++ library. The library linked by the ndk-build system is from the wrong location (platforms/android-9/arch-arm/usr/lib in my case).

include $(CLEAR_VARS)
LOCAL_MODULE := rightstdc
LOCAL_SRC_FILES := <path to the correct libstdc++.a>
include $(PREBUILT_STATIC_LIBRARY)

Add the module tag to the list of static libraries:

LOCAL_STATIC_LIBRARIES := engine shmem util rightstdc

The build/core/build-binary.mk prepends -L$(SYSROOT)/usr/lib if any libraries are specified in LOCAL_LDLIBS but in my case that is the wrong path.

I don't know if there is a missing step that should copy the correct libstdc++ to that location but the approach above will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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