Android NDK error: undefined reference - android

There have been some questions similar to mine, but it seems that their solutions don't work for me.
I'm trying to compile dumpsys source code using Android NDK. I have added a couple of lines to Android.mk to include the libraries.
The final Android.mk file looks like this:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
dumpsys.cpp
LOCAL_SHARED_LIBRARIES := \
libutils \
liblog \
libbinders
ANDROID_SRC="my android source directory"
LOCAL_C_INCLUDES := ${ANDROID_SRC}/frameworks/native/include \
${ANDROID_SRC}/system/core/include
#$(warning $(TARGET_C_INCLUDES))
LOCAL_MODULE:= dumpsys
TARGET_ARCH := arm
TARGET_ARCH_ABI := armeabi-v7a
include $(BUILD_EXECUTABLE)
when I execute ndk-build, I get the following errors:
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:144: error: undefined reference to 'android::String16::~String16()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:240: error: undefined reference to 'android::VectorImpl::finish_vector()'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:241: error: undefined reference to 'android::VectorImpl::~VectorImpl()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:135: error: undefined reference to 'android::String16::String16()'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:154: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/TypeHelpers.h:166: error: undefined reference to 'android::String16::String16(android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/String16.h:178: error: undefined reference to 'strzcmp16'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:32: error: undefined reference to 'android::defaultServiceManager()'
jni/dumpsys.cpp:35: error: undefined reference to '__android_log_print'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:224: error: undefined reference to 'android::VectorImpl::VectorImpl(unsigned int, unsigned int)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:245: error: undefined reference to 'android::VectorImpl::operator=(android::VectorImpl const&)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:378: error: undefined reference to 'android::VectorImpl::sort(int (*)(void const*, void const*))'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:49: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:51: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::String16(char const*)'
/home/mahdi/university/androidsource/system/core/include/utils/Vector.h:338: error: undefined reference to 'android::VectorImpl::add(void const*)'
jni/dumpsys.cpp:53: error: undefined reference to 'android::String16::~String16()'
jni/dumpsys.cpp:66: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:81: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
jni/dumpsys.cpp:89: error: undefined reference to 'android::operator<<(android::TextOutput&, android::String16 const&)'
/home/mahdi/university/androidsource/system/core/include/utils/StrongPointer.h:143: error: undefined reference to 'android::RefBase::decStrong(void const*) const'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aerr'
jni/dumpsys.cpp:94: error: undefined reference to 'android::aout'
collect2: error: ld returned 1 exit status
How should I solve this issue?
Thanks in advance.

I'm working on a similar issue here.
Basically, dumpsys is an AOSP component and intended to be built with the AOSP toolchain. You will need to apply some tweaks to port it to NDK – including stuff from ${ANDROID_SRC} is the first step but not the whole story.
You've successfully included the headers and thus made the compiler happy. Now the linker is complaining because it cannot find the libraries you are linking against. The good news is that they are shared libraries, thus having the libraries around at build time isn't a strict requirement.
The NDK defines a stable API of libraries you can use, which is documented here. liblog is in that list and can be included by adding the following lines to Android.mk:
LOCAL_LDLIBS := \
-llog \
The other two libraries are not part of the stable API. This essentially means that even if your code works on a particular version of Android, it may break on any later version because the API may have changed – you might want to bear this in mind.
Since these libraries are shared, all ld does is check if they actually provide the functions you're using. This question and its accepted answer have instructions for getting rid of the related error messages:
One way is to use something like:
LOCAL_LDFLAGS := -Wl,--unresolved-symbols=ignore-all
However, this will bypass all checks – so if you try to use a function that is indeed absent from the library, ld has no chance of warning you.
The cleaner, but more work-intensive approach, is to provide stub libraries. A stub library is essentially a dummy library which defines the same symbols (functions etc.) as the "real" thing but has no implementation (functions simply return without doing anything). It's enough to make the linker happy, but the libraries are not shipped and their "real" counterparts are used at runtime.
You would need to get the source code for the two libraries, which reside in the following directories: libutils and libbinders
system/core/libutils
frameworks/native/libs/binder
Copy these two dirs into your project's jni dir. Then strip the code down:
* Edit Android.mk, removing all build targets other than BUILD_SHARED_LIBRARY.
* Edit the source code files, replacing all function bodies with a simple return. It doesn't matter what you return, as long as you get the code to compile.
Eventually you will probably need to prevent the stub libraries from getting included in your .apk (I have yet to figure out how to do that).

Related

Undefined reference to isfinite

I had following error when done cross compilation for android.
Cross compiler used SDK/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin/arm-linux-androideabi'
./libgpsd.a(libgpsd_core.o):libgpsd_core.c:function gpsd_poll: error: undefined reference to '__isfinite'
./libgpsd.a(libgpsd_core.o):libgpsd_core.c:function gpsd_poll: error: undefined reference to '__isfinite'
./libgpsd.a(libgpsd_core.o):libgpsd_core.c:function gpsd_poll: error: undefined reference to '__isfinite'
./libgpsd.a(libgpsd_core.o):libgpsd_core.c:function gpsd_poll: error: undefined reference to '__isfinite'
The function infinite() is part of C99. Add LOCAL_CFLAGS += -std=c99 to your Android.mk. See question How to set standard c99 for compile android NDK project

Link libmpich to android NDK

I have generated Uclib using buildroot and used it to compile mpich for ARM devices.Then I created Android project and copied libmpich.a and the Include folder to the project here is the Android.mk:
LOCAL_PATH := $(call my-dir)
# static library info
LOCAL_MODULE_FILENAME:= libmpich
LOCAL_MODULE := libmpich
LOCAL_SRC_FILES := ../prebuilt/libmpich.a
LOCAL_EXPORT_C_INCLUDES := ../prebuilt/include
LOCAL_STATIC_LIBRARIES := libmpich
include $(PREBUILT_STATIC_LIBRARY)
# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuilt/include
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c
LOCAL_STATIC_LIBRARIES := libmpich
include $(BUILD_SHARED_LIBRARY)
after ndk-build i got the following error
jni/../prebuilt/libmpich.a(initthread.o):initthread.c:function PMPI_Init_thread: error: undefined reference to 'MPL_env2bool'
jni/../prebuilt/libmpich.a(msgprint.o):msgprint.c:function MPIU_Usage_printf: error: undefined reference to 'stdout'
jni/../prebuilt/libmpich.a(msgprint.o):msgprint.c:function MPIU_Error_printf: error: undefined reference to 'stderr'
jni/../prebuilt/libmpich.a(msgprint.o):msgprint.c:function MPIU_Internal_error_printf: error: undefined reference to 'stderr'
jni/../prebuilt/libmpich.a(msgprint.o):msgprint.c:function MPIU_Internal_sys_error_printf: error: undefined reference to 'stderr'
jni/../prebuilt/libmpich.a(msgprint.o):msgprint.c:function MPIU_Msg_printf: error: undefined reference to 'stdout'
jni/../prebuilt/libmpich.a(dbg_printf.o):dbg_printf.c:function MPIU_dbg_printf: error: undefined reference to 'stdout'
jni/../prebuilt/libmpich.a(dbg_printf.o):dbg_printf.c:function MPIU_dump_dbg_memlog_to_stdout: error: undefined reference to 'stdout'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2int'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2int'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2int'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2int'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2bool'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2bool'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2bool'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2str'
jni/../prebuilt/libmpich.a(param_vals.o):param_vals.c:function MPIR_Param_init_params: error: undefined reference to 'MPL_env2str'
jni/../prebuilt/libmpich.a(mpid_abort.o):mpid_abort.c:function MPID_Abort: error: undefined reference to 'stderr'
jni/../prebuilt/libmpich.a(mpid_vc.o):mpid_vc.c:function MPIDI_Populate_vc_node_ids: error: undefined reference to '__errno_location'
jni/../prebuilt/libmpich.a(mpid_vc.o):mpid_vc.c:function MPIDI_Populate_vc_node_ids: error: undefined reference to '__ctype_b'
jni/../prebuilt/libmpich.a(init.o):init.c:function PMPI_Init: error: undefined reference to 'MPL_env2str'
jni/../prebuilt/libmpich.a(strerror.o):strerror.c:function MPIU_Strerror: error: undefined reference to '__xpg_strerror_r'
jni/../prebuilt/libmpich.a(simple_pmi.o):simple_pmi.c:function accept_one_connection: error: undefined reference to '__errno_location'
jni/../prebuilt/libmpich.a(simple_pmi.o):simple_pmi.c:function PMI_Init: error: undefined reference to '__errno_location'
jni/../prebuilt/libmpich.a(sock.o):sock.c:function MPIDU_Socki_event_enqueue.isra.1: error: undefined reference to '__errno_location'
jni/../prebuilt/libmpich.a(sock.o):sock.c:function MPIDU_Sock_listen: error: undefined reference to 'MPL_env2range'
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi/libndk1.so] Error 1
I don't understand why it is not linking correctly and what is missing
Update :
when I add libmpl.a to the project all MPL errors disappeared
I think your problem occured because your static lib libmpich.a has dependance on another lib.
You have two diffenrent way to solve this issue :
Import any library needed by your libmpich.a as static library in your Android.mk as static library. The problem of this approach is that can be very long and painful if you have a lot dependance.
Build libmpich as a dynamic library, as dynamic library are package with their dependance. I do know how you build you libmpich library so i don't know if this will be hard to achieve. Once you have libmpich.so import it in your Android.mk as a dynamic library instead of a static one.

Sample Android NDK hello-gl2 unable to compile on Android Studio

I'm able to run the hello-jni of the samples provided in the NDK but I can't compile the hello-gl2 sample.
Could you help me? (I think is a problem linking with OpenGL)
This are the errors reported by Android Studio:
C:\AndroidstudioProjects\hello-gl2\app\src\main\jni\gl_code.cpp
Error:(39) undefined reference to `glGetError'
Error:(41) undefined reference to `__android_log_print'
Error:(40) undefined reference to `glGetError'
Error:(34) undefined reference to `glGetString'
Error:(35) undefined reference to `__android_log_print'
Error:(58) undefined reference to `glCreateShader'
Error:(60) undefined reference to `glShaderSource'
Error:(61) undefined reference to `glCompileShader'
Error:(63) undefined reference to `glGetShaderiv'
Error:(66) undefined reference to `glGetShaderiv'
Error:(70) undefined reference to `glGetShaderInfoLog'
Error:(72) undefined reference to `__android_log_print'
Error:(75) undefined reference to `glDeleteShader'
...
In your Android.mk file (in the /jni folder), there should be a LOCAL_LDLIBS line. Some of the libraries that come bundled with Android need to be indicated here. Try the following
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
Or at least add the options -lEGL and -lGLESv1_CM. The first is the EGL library, and the second is the GLES library.
I don't know if the last one is the right version number for your project though.
Finally I used eclipse (for those that are dealing with the same problems) and with Eclipse all went fine.

Building ImageMagick for Android

I am using https://github.com/lilac/Android-ImageMagick to build an ImageMagick library for Android. When trying to ndk-build ImageMagick I end up with the below errors.
primary0:AndroidMagickActivity primary0$ ~/ndk/ndk-build
SharedLibrary : libandroid-magick.so
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:988: error: undefined reference to 'jpeg_std_error'
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:995: error: undefined reference to 'jpeg_destroy_decompress'
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:1004: error: undefined reference to 'jpeg_CreateDecompress'
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:1006: error: undefined reference to 'jpeg_set_marker_processor'
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:1007: error: undefined reference to 'jpeg_set_marker_processor'
/Users/primary0/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/libcoders.a(jpeg.o): in function ReadJPEGImage:jni/../ndk-modules/ImageMagick-6.7.3-0/coders/jpeg.c:1008: error: undefined reference to 'jpeg_set_marker_
.
.
.
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libandroid-magick.so] Error 1
primary0:AndroidMagickActivity primary0$
The header file that defines the constants listed in the error is included jpeg.c
#include "jpeglib.h"
Any of you guys have an idea why I'm getting the undefined reference error?
The linker is not looking for "constants" it's looking for jump addresses to call functions. Your code obviously uses functions which are not available in the object code. This can have various reasons. Either you did not tell the compiler to use a library which is availabe or (in your case more likely) you did not tell the compiler that jpeg.c also needs to be compiled for this shared object. But this is difficult to diagnose without seeing the makefile.
Taking a short look at the ndk-modules folder of the git repository I get the feeling you need to build multiple modules. Maybe you just do it in the wrong order. Try building jpeg first.

Link error on Android

When compiling native code on android I am experiencing a problem :
CMakeFiles/test_v_job.dir/test_v_job.cpp.o:test_v_job.cpp:function
boost::detail::signal_handler::~signal_handler(): error: undefined
reference to 'sigaltstack'
CMakeFiles/test_v_job.dir/test_v_job.cpp.o:test_v_job.cpp:function
boost::detail::signal_handler::signal_handler(bool, int, bool, char*):
error: undefined reference to 'sigaltstack'
CMakeFiles/test_v_job.dir/test_v_job.cpp.o:test_v_job.cpp:function
boost::detail::signal_handler::signal_handler(bool, int, bool, char*):
error: undefined reference to 'sigaltstack' collect2: ld returned 1
exit status
signal.h header file is in the directory : C:\AndroidSDKS\android-ndk-r7-crystax\platforms\android-9\arch-arm\usr\include
What must be linked?
this error is caused probably by old platform of android-ndk,it should be compiled with at least android-9 or higher
it has nothing to do with boost libs
for example if you use ndk-build you schould add to Application.mk those lines:
APP_ABI := armeabi
APP_PLATFORM := android-9
To build the boost under ANDROID NDK I use github.com/MysticTreeGames/Boost-for-Android – e.proydakov

Categories

Resources