android ndk undefined reference to a method

情到浓时终转凉″ 提交于 2019-12-12 12:49:57

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13080345/android-ndk-undefined-reference-to-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!