Using the glib on Android : undefined reference to 'g_thread_init' - android

I am trying to compile a module for android using the ndk. The library has a dependency on glib, so I built that using https://github.com/ieei/glib, and everything seemed ok. However, when I try to use the glib module in my Android.mk file I get the following on running ndk-build:
jni/fluidsynth/fluidsynth/fluidsynth/src/fluid_cmd.c:1837: error: undefined reference to 'g_thread_init'
jni/fluidsynth/fluidsynth/fluidsynth/src/fluid_event.c:647: error: undefined reference to 'g_thread_init'
jni/fluidsynth/fluidsynth/fluidsynth/src/fluid_midi_router.c:106: error: undefined reference to 'g_thread_init'
jni/fluidsynth/fluidsynth/fluidsynth/src/fluid_sys.h:161: error: undefined reference to 'g_thread_init'
I looked up solutions to this problem, and I found this: http://ragnermagalhaes.blogspot.com/2007/09/undefined-reference-to-gthreadinit.html, but couldn't seem to get either of the proposed solutions to do anything for me (possibly because I am pretty new to the whole makefile thing)
My Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../PdCore/jni/libpd/pure-data/src \
$(LOCAL_PATH)/../glib \
$(LOCAL_PATH)/../glib/glib \
$(LOCAL_PATH)/../glib/gnulib \
$(LOCAL_PATH)/../glib/android \
$(LOCAL_PATH)/fluidsynth/fluidsynth/fluidsynth/include/fluidsynth \
$(LOCAL_PATH)/fluidsynth/fluidsynth/fluidsynth/include
LOCAL_MODULE := soundfonts
LOCAL_CFLAGS := -DPD
LOCAL_CFLAGS += -std=c11
LOCAL_CFLAGS += -w
LOCAL_CFLAGS +=
FLUID_SRC := $(wildcard $(LOCAL_PATH)/fluidsynth/fluidsynth/fluidsynth/src/*.c)
LOCAL_SRC_FILES := $(FLUID_SRC:$(LOCAL_PATH)/%=%) \
soundfonts.c
LOCAL_LDLIBS := $(LOCAL_PATH)/../glib/libs/armeabi/libglib-2.0.so
LOCAL_LDLIBS += -L$(LOCAL_PATH)/../PdCore/libs/$(TARGET_ARCH_ABI) -lpd -lgthread-2.0 -lglib-2.0
include $(BUILD_SHARED_LIBRARY)
Anybody have ideas for how to get rid of the linker errors? Thanks.

I figured it out! There are actually several shared object libraries being created when you build glib, and the one I was missing was libgthread-2.0.so.
I added libgthread-2.0.so to my LOCAL_LDLIBS and it worked

Related

ANDROID: How to properly link against static libraries while creating shared libraries with dependencies on the static ones

I have to use some c++ code in my android application. This code was used successfully in an iOS project.
The code depends on 2 external libraries: zero-mq and protocol buffers.
I compiled the zmq library as an static library like explained here. I added the static (.a) library and the .jar to my project.
I created the protobuf library with the following configurations:
./configure --host=arm-eabi --with-sysroot=x/android-ndk-r10d/platforms/android-21/arch-arm CC="x/android-ndk-r10d/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot x/android-ndk-r10d/platforms/android-21/arch-arm" --enable-cross-compile --with-protoc=protoc LIBS=-lc
make
I changed the real directories to x to make them shorter.
In my Android Project(IDE: Android Studio) I prepared everything which is necessary. I created a JNI Folder and deactivated the auto-creation of the makefiles.
Application.mk:
APP_MODULE := proxy
APP_STL := gnustl_shared
APP_CPPFLAGS := -frtti -fexceptions --std=c++11
APP_ABI := armeabi-v7a ##all later
NDK_TOOLCHAIN_VERSION := 4.9
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := zmq_static
LOCAL_SRC_FILES := zmq/libzmq.a
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static1
LOCAL_SRC_FILES := protobuf/libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static2
LOCAL_SRC_FILES := protobuf/libprotobuf-lite.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
LOCAL_MODULE := protobuf_static3
LOCAL_SRC_FILES := protobuf/libprotoc.a
LOCAL_EXPORT_C_INCLUDES := google/protobuf protobuf/
include $(PREBUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := proxy
LOCAL_CFLAGS := -I/include -pthread -lpthread -D__GXX_EXPERIMENTAL_CXX0X__ - frtti
LOCAL_CPPFLAGS := -I/include -pthread -lpthread -D__GXX_EXPERIMENTAL_CXX0X__ -frtti
LOCAL_CPP_FEATURES += exceptions
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := \
usersession.cpp\
## LOCAL_ALLOW_UNDEFINED_SYMBOLS := true will compile the code but shutdown on runtime
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\main\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\arm\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\debug\jni
LOCAL_C_INCLUDES += C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\armDebug\jni
LOCAL_C_INCLUDES += \zmq
LOCAL_C_INCLUDES += \protobuf
LOCAL_STATIC_LIBRARIES := zmq_static protobuf_static1 protobuf_static2 protobuf_static3
LOCAL_WHOLE_STATIC_LIBRARIES := zmq_static protobuf_static1 protobuf_static2 protobuf_static3
include $(BUILD_SHARED_LIBRARY)
The zmq library is in the subdirectory zmq and the protobuf library is in the subfolder protobuf.
Now the linking of the Objects still does not work. The Error Output when I execute ndk-build:
C:\Users\M\Dropbox\Workspace\ndk_swig_test\app\src\main\jni>ndk-build
[armeabi-v7a] SharedLibrary : libproxy.so
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/ext/new_allocator.h:127: error: undefined reference to 'ControlledInstance::ControlledInstan (std::shared_ptr<protogen::Application>, std:
:shared_ptr<protogen::Role>, std::shared_ptr<protogen::User>)'
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/bits/shared_ptr_base.h:511: error: undefined reference to 'protogen::User::User()'
C:/Users/M/Documents/ndk/sources/cxx-stl/gnu- libstdc++/4.9/include/bits/shared_ptr_base.h:914: error: undefined reference to 'google::protobuf::internal::empty tring_'
C:/Users/M/Dropbox/Workspace/ndk_swig_test/app/src/main//jni/controlledinstance.h :23: error: undefined reference to 'protogen::MetaGraph::~MetaGraph()'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [C:/Users/M/Dropbox/Workspace/ndk_swig_test/app/src/main//obj/local/armeabi- v7a/libproxy.so] Error 1
I tried many versions of the Android.mk and recreated the library more than once with different options which I found all over the internet.
I also looked at dozens of threads on stackoverflow which did not help me.(I'm not allowed to link them because of low reputation)
Additionally i read most of the doc files from the ndk e.g. PREBUILTS.
I added some other directories to my JNI directory e.g. the directory with the original files and directories (compiler, io, stubs...). I think this directory should offer the export of the necessary methods if the prebuild library was successfully linked to my shared library - which is not the case.
I tried far more than I can explain in few minutes and I think it would be overkill if i added everything I've tried because nothing helped.
Because this is my first question I dont have the reputation to include more than 2 links. Sorry for that.
There may probably be other issues as well, but you at least have got a typo - it should be include $(PREBUILT_STATIC_LIBRARY), as in, BUILT, not BUILD.

Cannot build local shared library in Android Ndk

I want to build library .so for version 4.0.3 but I am unable to do so.
What I feel is that these problems are caused because my .mk file is not
linking with the libraries.
Android.mk file
Binder.cpp \
BpBinder.cpp \
CursorWindow.cpp \
IInterface.cpp \
IMemory.cpp \
IPCThreadState.cpp \
IPermissionController.cpp \
IServiceManager.cpp \
MemoryDealer.cpp \
MemoryBase.cpp \
MemoryHeapBase.cpp \
MemoryHeapPmem.cpp \
Parcel.cpp \
PermissionCache.cpp \
ProcessState.cpp \
Static.cpp
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS += -lpthread
LOCAL_MODULE := libbinder1
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
LOCAL_SRC_FILES := $(sources)
include $(BUILD_SHARED_LIBRARY)
#include $(CLEAR_VARS)
#LOCAL_CFLAGS += -DHAVE_PTHREADS
#LOCAL_LDLIBS += -lpthread
#LOCAL_MODULE := libbinder
#LOCAL_SRC_FILES := $(sources)
#include $(BUILD_STATIC_LIBRARY)
This file builds static i.e .a file for me but shows following errors while building shared library.
[armeabi] Compile++ thumb: binder1 <= IPCThreadState.cpp
jni/IPCThreadState.cpp:292:8: error: 'pthread_mutex_t' does not name a type
jni/IPCThreadState.cpp:294:8: error: 'pthread_key_t' does not name a type
jni/IPCThreadState.cpp: In static member function 'static android::IPCThreadState* android::IPCThreadState::self()':
I fixed above errors using
LOCAL_CFLAGS += -DHAVE_PTHREADS
But now, at the time of generating library I am getting a huge list of errors.
D:/android-ndk-r9c-windows-x86/android-ndk-r9c/toolchains/arm-linux-androideabi- 4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux- androideabi/bin/ld.exe: error: cannot find -lpthread
D:/android-ndk-r9c-windows-x86/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/binder1/Binder.o: in function android::Vector<android::String16>::do_copy(void*, void const*, unsigned int) const:jni/utils/TypeHelpers.h:142: error: undefined reference to 'android::String16::String16(android::String16 const&)'
Any help will be appreciated.
Android NDK supports pthreads, but does not provide libpthread as usual in Linux toolchains. Your first error message will be gone if you use
LOCAL_CFLAGS += -DHAVE_PTHREADS
and not add LOCAL_LDLIBS += -lpthread
Regarding the undefined reference to do_copy(), it comes from system library libutils.so. It is not safe to use libraries that are not officially published with NDK (see more here), so you better rewrite this piece of code.
Probably you received your Android.mk file from the google source or one of its forks. I doubt that the resulting library will be useable, because the original libbinder.so requires system app with elevated permissions will be loaded when your app starts.
Anyways, referring to system libraries as LOCAL_SHARED_LIBRARIES does not work with ndk-build. Instead of LOCAL_SHARED_LIBRARIES := liblog libcutils libutils you are expected to write
LOCAL_LDLIBS += -llog -lcutils -lutils

NDK error wscicmp was not declared in this scope

Getting the following error:
wscicmp was not declared in this scope
Android NDK's Application.mk contains:
APP_STL := stlport_static
Android.mk:
LOCAL_CFLAGS := -DANDROID_NDK \
-D_STLP_HAS_WCHAR_T \
-DDISABLE_IMPORTGL
Using NDK r7.
The function wcsicmp is mostly a Windows-specific function. Try using the wcscasecmp function?

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.

Undefined reference error using static library to build shared library

I am confused If I am doing something wrong while using the static library.
For testing,
I'm trying to make a shared library(pal) using a static library(mtwist).
But I am getting undefined reference to the functions of the static library(mtwist)
Compile thumb : mtwist <= mtwist.c
Compile thumb : mtwist <= randistrs.c
StaticLibrary : libmtwist.a
SharedLibrary : libpal.so
F:/afe/obj/local/armeabi/objs-debug/pal/PALrandom.o: In function `CRandom':
F:/afe/jni/PAL/Sources/PALrandom.cpp:47: undefined reference to `mts_seed'
F:/afe/jni/PAL/Sources/PALrandom.cpp:48: undefined reference to `mts_mark_initialized'
F:/afe/jni/PAL/Sources/PALrandom.cpp:47: undefined reference to `mts_seed'
F:/afe/jni/PAL/Sources/PALrandom.cpp:48: undefined reference to `mts_mark_initialized'
F:/afe/obj/local/armeabi/objs-debug/pal/PALrandom.o: In function `PAL::CRandom::
GetRandomInteger16()':
F:/afe/jni/PAL/Sources/PALrandom.cpp:60: undefined reference to `rds_iuniform'
F:/afe/obj/local/armeabi/objs-debug/pal/PALrandom.o: In function `PAL::CRandom::GetRandomInteger32()':
F:/afe/jni/PAL/Sources/PALrandom.cpp:67: undefined reference to `rds_iuniform'
F:/afe/jni/PAL/Sources/PALrandom.cpp:69: undefined reference to `rds_iuniform'
collect2: ld returned 1 exit status
make: *** [/cygdrive/f/afe/obj/local/armeabi/libpal.so] Error 1
**Look libmtwist.a has been built though its giving undefined reference for the mtwist functions.
This is the Android.mk file mtwist(Static Library):
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mtwist
LOCAL_SRC_FILES := mtwist.c \
randistrs.c \
include $(BUILD_STATIC_LIBRARY)
This is the Android.mk file of pal (Shared Library):
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../Headers \
$(LOCAL_PATH)/../../BaseMulti/Headers \
$(LOCAL_PATH)/../../boost \
$(LOCAL_PATH)/../../mtwist-0.7
LOCAL_CFLAGS += -Wno-deprecated -D_LINUX -DBOOST_HAS_PTHREADS -fexceptions
LOCAL_MODULE := pal
LOCAL_SRC_FILES := PALcritsec.cpp \
PALdebug.cpp \
PALdebuglog.cpp \
PALinet.cpp \
PALlocalhostUdpEvent.cpp \
PALnetwork.cpp \
PALpollarray.cpp \
PALprofiler.cpp \
PALrandom.cpp \
PALserializable.cpp \
PALsocket.cpp \
PALstringutil.cpp \
PALsystime.cpp \
PALthread.cpp \
LOCAL_SHARED_LIBRARIES := boostthread
LOCAL_WHOLE_STATIC_LIBRARIES := mtwist
include $(BUILD_SHARED_LIBRARY)
and this is the main Android.mk file:
ROOT_DIR := $(call my-dir)
include $(ROOT_DIR)/PAL/Sources/Android.mk
include $(ROOT_DIR)/mtwist-0.7/Android.mk
include $(ROOT_DIR)/boost/libs/thread/src/Android.mk
include $(ROOT_DIR)/PAL/Sources/Android.mk
include $(ROOT_DIR)/mtwist-0.7/Android.mk
include $(ROOT_DIR)/boost/libs/thread/src/Android.mk
Did you trie to change build order for me it looks like you try to build pal, before mtwist, and i think boost should be at top of it.
include $(ROOT_DIR)/boost/libs/thread/src/Android.mk
include $(ROOT_DIR)/mtwist-0.7/Android.mk
include $(ROOT_DIR)/PAL/Sources/Android.mk
And why use some LOCAL_WHOLE_STATIC_LIBRARIES that's not documented at least not in r5b? ( or at least i can not find ;] )
I would use something like this:
LOCAL_SHARED_LIBRARIES := boostthread
LOCAL_STATIC_LIBRARIES := mtwist

Categories

Resources