Skip to content

Android NDK 使用.a文件

一个编译时使用.a文件的例子。流程是准备好库文件以及对应的头文件,编写makefile,编译。

准备库文件与头文件

先准备头文件和.a文件。这里以ncnn目录为例。

|-- Android.mk
|-- Application.mk
|-- ncnn
|   |-- include
|   |   |-- benchmark.h
|   |   |-- ....h # 对应的头文件
|   |-- lib
|   |   `-- libncnn.a

编写makefile

Android.mk中声明库文件。 这里是引入opencv的库。

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := ncnn
LOCAL_SRC_FILES := ncnn/lib/libncnn.a
include $(PREBUILT_STATIC_LIBRARY)

LOCAL_STATIC_LIBRARIES := ncnn

OPENCVROOT:=$(LOCAL_PATH)/../../../../../../OpenCV-android-sdk
OPENCV_CAMERA_MODULES:=off
OPENCV_INSTALL_MODULES:=on
#OPENCV_LIB_TYPE:=SHARED
OPENCV_LIB_TYPE:=STATIC
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk

# v20 with sse
LOCAL_SRC_FILES :=  v20/fdssttracker.cpp v20/fhog.cpp v20/runtracker.cpp \
                     jni_eman.cpp \
                     ncnn/ssdmobilenet.cpp jni_encnn.cpp

# 后面是模块的配置

这里要注意,头文件和.a文件必须是对应的。否则编译时会报错error: undefined reference to

E:/projects/myProj/myProject/f1/src/main/jni/ncnn/lib/libncnn.a(net.cpp.o):net.cpp:function ncnn::Net::load_model(__STDIO_FILE_STRUCT*): error: undefined reference to 'stderr'
E:/projects/myProj/myProject/f1/src/main/jni/ncnn/lib/libncnn.a(net.cpp.o):net.cpp:function ncnn::Net::load_model(char const*): `error: undefined reference to`
'stderr'
E:/projects/myProj/myProject/f1/src/main/jni/ncnn/lib/libncnn.a(net.cpp.o):net.cpp:function ncnn::Net::load_model(unsigned char const*): error: undefined reference to 'stderr'
E:/projects/myProj/myProject/f1/src/main/jni/ncnn/lib/libncnn.a(net.cpp.o):net.cpp:function ncnn::Net::find_blob_index_by_name(char const*) const: error: undefined reference to 'stderr'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [E:/projects/myProj/myProject/f1/src/main/obj/local/armeabi-v7a/libf1.so] Error 1