Wednesday, 28 March 2018

c++ - Use ndk-build to link with an existing static library

I have a library that I compile for Android using cmake and android-cmake and obtaining a static lib.



Then I tried to link my test project with this static library using such an Android.mk file:



LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := ../test.cxx
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../src
LOCAL_CFLAGS := -std=gnu++11 -D__ANDROID__

LOCAL_CPP_FEATURES += exceptions
LOCAL_LDLIBS += -L.. -ljson++
include $(BUILD_EXECUTABLE)


And Application.mk



NDK_TOOLCHAIN_VERSION = 4.7
APP_STL := gnustl_static



json++ here is a name of a library I've previously built with cmake.



It fails during linking with




../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_invalid_type(void const*, json::object_type, json::object_type, char const*): error: undefined reference to 'std::basic_ostringstream, std::allocator >::~basic_ostringstream()'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_invalid_type(void const*, json::object_type, json::object_type, char const*): error: undefined reference to 'VTT for std::basic_ostringstream, std::allocator >'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_invalid_type(void const*, json::object_type, json::object_type, char const*): error: undefined reference to 'vtable for std::basic_ostringstream, std::allocator >'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_invalid_type(void const*, json::object_type, json::object_type, char const*): error: undefined reference to 'vtable for std::basic_stringbuf, std::allocator >'

../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): error: undefined reference to 'std::basic_ostringstream, std::allocator >::~basic_ostringstream()'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): error: undefined reference to 'VTT for std::basic_ostringstream, std::allocator >'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): error: undefined reference to 'vtable for std::basic_ostringstream, std::allocator >'
../libjson++.a(object.cpp.o):object.cpp:function json::error_json_object_no_such_key(void const*, void const*, unsigned int): error: undefined reference to 'vtable for std::basic_stringbuf, std::allocator >'
../libjson++.a(object.cpp.o):object.cpp:function std::basic_stringbuf, std::allocator >::~basic_stringbuf(): error: undefined reference to 'vtable for std::basic_stringbuf, std::allocator >'
collect2: error: ld returned 1 exit status
make: * [obj/local/armeabi/test] Error 1




This error is caused by libgnustl_static.a being in call of compiler before the -ljson++:




/opt/android-ndk/toolchains/arm-linux-androideabi-4.7/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/opt/android-ndk/platforms/android-3/arch-arm ./obj/local/armeabi/objs/test/__/test.o /opt/android-ndk/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi/libgnustl_static.a -lgcc -no-canonical-prefixes  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now  -L/opt/android-ndk/platforms/android-3/arch-arm/usr/lib -L.. -ljson++ -lc -lm -o ./obj/local/armeabi/test


So it can be solved by adding



 -L${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/ -lgnustl_static


to LOCAL_LDLIBS




And the question now: what is the correct way of linking with in existing static library if it does not belong to my project and is compiled with different build system?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...