Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android NDK, two Static Libraries and Linking

I started off creating libraries as shared libraries, but I considered it would be more efficient to create one shared libraries and the rest static. When it was all shared, it compiled and linked fine, but moving to static, I get on linking "undefined reference".

Edit: I build all the libraries in one Android.mk

Android.mk:

MY_LOCAL_PATH := $(call my-dir)
MY_LOCAL_CFLAGS := -DDEBUG

TARGET_PLATFORM := 'android-4'

LOCAL_PATH := $(MY_LOCAL_PATH)/../../Base

include $(CLEAR_VARS)

LOCAL_MODULE     := Base
LOCAL_SRC_FILES  := <Base src files>
include $(BUILD_STATIC_LIBRARY)

MY_LOCAL_STATIC_LIBRARIES := Base

MY_LOCAL_C_INCLUDES := $(MY_LOCAL_PATH)/../../Base

LOCAL_PATH := $(MY_LOCAL_PATH)/../../Framework

include $(CLEAR_VARS)

LOCAL_MODULE     := Framework
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)
LOCAL_SRC_FILES  := <Framework src files>
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)

include $(BUILD_STATIC_LIBRARY)

MY_LOCAL_STATIC_LIBRARIES += Framework

MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/../../Framework

LOCAL_PATH := $(MY_LOCAL_PATH)/Graphics

include $(CLEAR_VARS)

LOCAL_MODULE    := Graphics
LOCAL_SRC_FILES := <Graphics src files>
LOCAL_EXPORT_LDLIBS := -lGLESv1_CM
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)

include $(BUILD_STATIC_LIBRARY)

MY_LOCAL_STATIC_LIBRARIES += Graphics

MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/Graphics

LOCAL_PATH := $(MY_LOCAL_PATH)/Platform

include $(CLEAR_VARS)

LOCAL_MODULE := Platform
LOCAL_SRC_FILES := <Platform src files>
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)

include $(BUILD_STATIC_LIBRARY)

MY_LOCAL_STATIC_LIBRARIES += Platform

MY_LOCAL_C_INCLUDES += $(MY_LOCAL_PATH)/Platform

LOCAL_PATH := $(MY_LOCAL_PATH)

include $(CLEAR_VARS)

LOCAL_MODULE    := Final
LOCAL_SRC_FILES := <Final src files>
LOCAL_STATIC_LIBRARIES := $(MY_LOCAL_STATIC_LIBRARIES)
LOCAL_LDLIBS    := -llog
LOCAL_CFLAGS := $(MY_LOCAL_CFLAGS)
LOCAL_C_INCLUDES := $(MY_LOCAL_C_INCLUDES)

include $(BUILD_SHARED_LIBRARY)

Last line of ndk-build V=1 -B:

SharedLibrary  : libFinal.so
/Users/robbie/Library/Frameworks/Android-NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/darwin-x86/bin/arm-linux-androideabi-g++ -Wl,-soname,libFinal.so -shared --sysroot=/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm <object files>   /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libBase.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libFramework.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libGraphics.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libPlatform.a /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libstdc++.a  /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libc.so /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libstdc++.so /Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib/libm.so   -Wl,--no-undefined -Wl,-z,noexecstack -L/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib -llog -lGLESv1_CM -lstdc++ -Wl,-rpath-link=/Users/robbie/Library/Frameworks/Android-NDK/platforms/android-4/arch-arm/usr/lib -lsupc++ -o /Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libFinal.so
/Users/robbie/Documents/Apps/Revolution/Android/obj/local/armeabi/libPlatform.a(ATexture.o): In function `ATexture':
/Users/robbie/Documents/Apps/Revolution/Android/jni/SpinTap/ATexture.cpp:9: undefined reference to `TextureRenderer::TextureRenderer(unsigned int)'
/Users/robbie/Documents/Apps/Revolution/Android/jni/SpinTap/ATexture.cpp:9: undefined reference to `TextureRenderer::TextureRenderer(unsigned int)'

Edit2: TextureRenderer is in Graphics, which is included.

Does anyone have an idea why it may not be working and how to fix it?

like image 719
NebulaFox Avatar asked Jan 22 '11 01:01

NebulaFox


2 Answers

This looks like an order-of-linking issue to me.

Your command line is:

arm-linux-androideabi-g++ -Wl,-soname,libFinal.so -shared \
  libBase.a libFramework.a libGraphics.a libPlatform.a -o libFinal.so

and the error is

libPlatform.a(ATexture.o): In function `ATexture':
ATexture.cpp:9: undefined reference to `TextureRenderer'
ATexture.cpp:9: undefined reference to `TextureRenderer'

TextureRenderer is in Graphics. But libGraphics is before libPlatform on the command line. g++ will search each library on the command line in the order they are given, loading functions to resolve external references. It will read libGraphics once, load the functions that resolve external references and move on to libPlatform.

Try changing LOCAL_STATIC_LIBRARIES := $(MY_LOCAL_STATIC_LIBRARIES) to LOCAL_STATIC_LIBRARIES := Platform Graphics Framework Base and see how you get on.

like image 184
richq Avatar answered Oct 06 '22 04:10

richq


In your Android.mk, make sure you are referencing the static library with the proper call:

LOCAL_STATIC_LIBRARIES := mystaticlibproj

before calling include $(BUILD_SHARED_LIBRARY).

Then, at the end of the file, place the call to import the static lib module

$(call import-module, mystaticlibproj)

If you're still having trouble, post the verbose build log (ndk-build V=1 -B) and your Android.mk

like image 38
levis501 Avatar answered Oct 06 '22 05:10

levis501