android ndk undefined reference to a method - android

Hi Sorry for the long post I am trying to compile some static classes namely jsmn.c,json.c and buf.c which are part of the jsmn json library I downloaded from https://github.com/alisdair/jsmn-example/downloads.
I am compiling two STATIC_LIBRARIES lib1 and json_librrary.lib1 has native code which is dependent on json_library.Then I am making two libraries into a shared library containing
gnustl_static AND lib1
My folder structure is as follows
jni/lib1/ANdroid.mk
include $(CLEAR_VARS)
LOCAL_MODULE := json_library
LOCAL_SRC_FILES := /3rdParty/jsmn/json_library.a
LOCAL_SRC_FILES := /3rdParty/jsmn/jsmn.c /3rdParty/jsmn/buf.c /3rdParty/jsmn/log.c /3rdParty/jsmn/json.c
LOCAL_C_INCLUDES := /3rdParty/jsmn/jsmn.h /3rdParty/jsmn/buf.h /3rdParty/jsmn/log.h /3rdParty/jsmn/json.h
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
# Module Name
LOCAL_MODULE := lib1
LOCAL_STATIC_LIBRARIES := json_library
........
.......
......
include $(BUILD_STATIC_LIBRARY)
jni/Android.mk
# Here we give our module name
LOCAL_MODULE := lib2
# list the static modules included here!!!
LOCAL_STATIC_LIBRARIES := gnustl_static lib1
....
include $(BUILD_SHARED_LIBRARY)
jni/Application.mk
APP_MODULES := lib2
# Optimization for release
APP_OPTM := release
#Targets
APP_ABI := armeabi-v7a armeabi
So inside the lib1 I have class which calls a method from the json library named json_tokenise
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <jsmn/jsmn.h>
#include <jsmn/json.h>
#include <jsmn/buf.h>
jsmntok_t *tokens=json_tokenise((char *)data);
typedef enum {
START,
WRAPPER,
MESSAGE,
ROUTE,
OBJECT,
ARRAY,
SKIP,
STOP
}parse_state;
I am getting the following error
undefined reference to `json_tokenise(char*)'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi-v7a/lib2.so] Error 1
**** Build Finished ****
When I have a look inside [obj/local/armeabi-v7a/ I can see
libjson_library.a liblib1.a libgnustl_static.a are getting generated for the armabi-v7 and it fails to generate lib2 because of the error.
Please help or guide me where I am going wrong I have spent two days on this and I am new to NDK.

You should fix your jni/lib1/Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := json_library
LOCAL_SRC_FILES := 3rdParty/jsmn/jsmn.c 3rdParty/jsmn/buf.c 3rdParty/jsmn/log.c 3rdParty/jsmn/json.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/3rdParty/jsmn
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)
If you still have problems after this, please run ndk-build with parameter V=1 on command line and post the full output of this build and full content of all your Android.mk files.

I faced similar problem. The json_library.a should be included in prebuild static library module. And that should be compiled for the desired platform.
include $(CLEAR_VARS)
LOCAL_MODULE := json_core
LOCAL_SRC_FILES := /3rdParty/jsmn/json_library.a
include $(PREBUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := json_library
LOCAL_SRC_FILES := /3rdParty/jsmn/jsmn.c /3rdParty/jsmn/buf.c /3rdParty/jsmn/log.c /3rdParty/jsmn/json.c
There is no need to include each file separately.
LOCAL_C_INCLUDES := /3rdParty/jsmn/
Finally, it is needed to specify that json_library module is dependent on json_core (prebuilt library) .
LOCAL_STATIC_LIBRARIES := json_core
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)
In this way the linker knows where to find all the definitions of methods for each module.

Related

Android NDK: Directive not visible to includes of static library

I simply want use a static library for an NDK project. The library works with build systems like automake, but in Android.mk I have a problem with the source files. For some reason, directives don't work over different header files and I get the following error:
error: 'myname' does not name a type
It seems that the directive defined in file1.hpp is not visible in file2.hpp which includes file1.hpp (as I said, this problem does not occur with other build systems like automake.
What am I doing wrong here?
Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE := libstat
LOCAL_LDLIBS := -lm
LOCAL_SRC_FILES := /home/dir/libstat.a
LOCAL_EXPORT_C_INCLUDES := /home/dir/src
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_CFLAGS := -lm -ldl /home/dir/src
LOCAL_LDFLAGS := -L/home/dir/
LOCAL_C_INCLUDES += /home/dir/src
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -ggdb
LOCAL_STATIC_LIBRARIES := libstat
include $(BUILD_SHARED_LIBRARY)
src includes all header and source files. Any hint would be appreciated.

Android NDK:linking static library with shared library

Iam trying to compile my native code. Here is my android.mk file
//part1-static lib
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := irrlicht
IRRLICHT_LIB_NAME := lib$(LOCAL_MODULE).a
LOCAL_C_INCLUDES := include
LOCAL_SRC_FILES := \
CColorConverter.cpp \
CCSMLoader.cpp \
CCubeSceneNode.cpp \
CD3D8Driver.cpp \
include $(BUILD_STATIC_LIBRARY)
//part-2 shared lib
include $(CLEAR_VARS)
LOCAL_MODULE := irrlichttest
LOCAL_SRC_FILES := test-app.cpp test.cpp android-receiver.cpp
LOCAL_C_INCLUDES := include
LOCAL_CFLAGS := -O3 -DANDROID_NDK -DDISABLE_IMPORTGL -I$(LOCAL_PATH)/../include/ - I./include/
LOCAL_CPPFLAGS := -Wno-error=format-security
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog -lGLESv2
LOCAL_STATIC_LIBRARIES := irrlicht
include $(BUILD_SHARED_LIBRARY)
and here is my application.mk
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-10
APP_MODULE := irrlicht irrlichttest
i want to compile "irrlicht" module first and then "irrlichttest' module. The problem iam facing is my irrlichttest module compile first and it start looking for reference and give me undefined reference error.Right now If i compile part1(static lib) only it successfully generate libirrlicht.a, But with part2 it start giving me error. What am i doing wrong.
You have one extra \ at the end of your first LOCAL_SRC_FILES definition, this makes the 'include $(BUILD_STATIC_LIBRARY)' part go into LOCAL_SRC_FILES, and the line is never parsed / executed. In other words, due to this your module definition for the 'iirlicht' module is completely ignored by ndk-build, hence the problem you're seeing.
Remove the \ after CD3D8Driver.cpp, and that should fix it.
NDK will compile the irrlichttest sources, then irrlich sources, then create libirrlich.a, and only after that it will link libirrlichttest.so. It is very insightful to run
ndk-build clean all V=1
and see in the build log which commands are actually executed to build the project.

ADT tool chain not producing output for static libraries

Trying to build a static NDK library using Android's ADT Eclipse tool chain. However, whenever I build with BUILD_STATIC_LIBRARY, no output is produced: I get the message
make: Nothing to be done for `all'."
Any recommendations?
LOCAL_PATH := $(call my-dir)
STL_PATH := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include"
PLATFORM_INCLUDE := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include"
APP_STL := gnustl_static
include $(CLEAR_VARS)
LOCAL_MODULE := libCore
LOCAL_CPPFLAGS += -std=c++11 -fexceptions -D_OS_ANDROID
LOCAL_LDLIBS := -lGLESv2 -lEGL -lstdc++
LOCAL_C_INCLUDES += $(LOCAL_PATH)/Headers
...
LOCAL_SRC_FILES += Source/Engine/Game.cpp
...
include $(BUILD_STATIC_LIBRARY)
Here is the content of Android.mk file of two-libs sample project from Android NDK.
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-first
LOCAL_SRC_FILES := first.c
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_STATIC_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)
You may try building the static library as part of another shared library as shown in the example.
I just did a ndk-build on the two-libs sample project and i could see the .a file along with .so in obj\local\armeabi directory.
Edit:
By default, ndk-build will only build shared libraries and executables, and the modules they depend on. To force a build specify libCore in APP_MODULES as follows.
APP_MODULES := libCore
or in command line as
ndk-build APP_MODULES=libCore

Headers with Android NDK

I'm trying to compilate my own library with the Android NDK But I've got some problems.
Here is my Android.mk file:
# Define vars for library that will be build statically.
include $(CLEAR_VARS)
LOCAL_MODULE := MyLib
LOCAL_SRC_FILES := ../../../src/mylib/utils/Timer.cpp
LOCAL_C_INCLUDES := ../../../src/mylib/
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_LDLIBS := -llog
LOCAL_CPPFLAGS := -std=c++0x
include $(BUILD_SHARED_LIBRARY)
When I build my project with "ndk-build" I've got the following error :
Clean: mylib [armeabi]
Clean: stlport_shared [armeabi]
Clean: stlport_static [armeabi]
Compile++ thumb : mylib <= Timer.cpp
jni/../../../src/mylib/utils/Timer.cpp:1:34: fatal error: mylib/utils/Timer.hpp: No such file or directory
compilation terminated.
For information, I'm including the .hpp like that :
#include <mylib/utils/Timer.hpp>
I don't know why headers aren't found, my library working in Xcode and eclipse.
Thanks for your time!
Edit: Here is my project's architecture to understand my problem: http://i.imgur.com/aiah6zH.jpg
I'm trying to indicate where is located my ".hpp" files in the Android.mk file.
Your LOCAL_C_INCLUDES should include the ../../../src/ or ../../../inc directory in order for you to use #include <mylib/utils/Timer.hpp> i.e:
LOCAL_C_INCLUDES := ../../../src/
Why don't you put your C and C++ headers and source files inside the jni/ directory of the Android project, near the Android.mk file?
See: What is the difference between #include <filename> and #include "filename"?
Also this is incorrect, because the second LOCAL_LDLIBS overrides the previous LOCAL_LDLIBS directive in the current module :
LOCAL_LDLIBS = -lz -lm
LOCAL_LDLIBS := -llog
If you want to append to a make directive use:
LOCAL_LDLIBS := -lz -lm
LOCAL_LDLIBS += -llog
or LOCAL_LDLIBS := -lz -lm -llog
EDIT:
Using the following Android.mk it seems to work if you run ndk-build from the Android/jni directory.
LOCAL_PATH := $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE := MyLib
LOCAL_C_INCLUDES := ../../../inc/
LOCAL_SRC_FILES := ../../../src/mylib/utils/Timer.cpp
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE := MyNativeFinalSharedLib
LOCAL_LDLIBS := -lz -lm -llog
LOCAL_CPPFLAGS := -std=c++0x
LOCAL_STATIC_LIBRARIES := MyLib
include $(BUILD_SHARED_LIBRARY)
Also you forgot to put LOCAL_PATH := (call my-dir) on the first line and some other missing make directives.
An Android.mk file must begin with the definition of the LOCAL_PATH
variable. It is used to locate source files in the development tree.
In this example, the macro function 'my-dir', provided by the build
system, is used to return the path of the current directory (i.e. the
directory containing the Android.mk file itself).
(from android-ndk-r8d/docs/ANDROID-MK.html)
It's highly recommended to have your Android.mk file define $(LOCAL_PATH). In your case, the best choice would probably be
LOCAL_PATH := $(call my-dir)/../../../../src
Now you can simply write
LOCAL_SRC_FILES := mylib/utils/Timer.cpp
But for the includes, the path should be defined as relative from current, i.e. MyProject/build/Android directory. In your case, it seems, the correct path would be
LOCAL_C_INCLUDES := ../../../inc
This when you specify mylib in the #include statement:
#include <mylib/utils/Timer.hpp>

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?
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.
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

Categories

Resources