android ndk undefined对方法的引用

时间:2012-10-26 03:30:31

标签: android android-ndk static-libraries undefined-reference android-ndk-r7

嗨很抱歉,我试图编译一些静态类,即jsmn.c,json.c和buf.c,它们是我从https://github.com/alisdair/jsmn-example/downloads下载的jsmn json库的一部分。

我正在编译两个STATIC_LIBRARIES lib1和json_librrary.lib1具有依赖于json_library的本机代码。然后我将两个库放入一个共享库中   gnustl_static AND lib1

我的文件夹结构如下

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

所以在lib1中我有一个类,它从名为 json_tokenise

的json库中调用一个方法
#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;

我收到以下错误

undefined reference to `json_tokenise(char*)'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi-v7a/lib2.so] Error 1

**** Build Finished ****

当我看一下[obj / local / armeabi-v7a /我可以看到 libjson_library.a liblib1.a libgnustl_static.a 正在为armabi-v7生成,但由于错误而无法生成lib2。

请帮助或指导我在哪里出错我已经花了两天时间在这上面,我是NDK的新手。

2 个答案:

答案 0 :(得分:4)

您应该修复jni/lib1/Android.mk文件:

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)

如果此后仍有问题,请在命令行中使用参数ndk-build运行V=1,并发布此版本的完整输出以及所有Android.mk个文件的完整内容。< / p>

答案 1 :(得分:0)

我遇到了类似的问题。 json_library.a应包含在预构建静态库模块中。这应该编译为所需的平台。

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 

无需单独包含每个文件。

LOCAL_C_INCLUDES := /3rdParty/jsmn/

最后,需要指定json_library模块依赖于json_core(预建库)。

LOCAL_STATIC_LIBRARIES := json_core 
# Optional compiler flags.
LOCAL_LDLIBS   = -lz -lm
LOCAL_CFLAGS   = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)

通过这种方式,链接器知道在哪里可以找到每个模块的所有方法定义。