android ndk undefined reference to a method

11.5k views Asked by At

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.

2

There are 2 answers

0
Alex Cohn On

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.

0
KRoy On

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.