首先,我想说我已经在SO和其他互联网上尝试了所有其他解决方案。
我的目标是为Android构建dlib作为静态库,并创建共享对象作为JNI的包装。
第一部分是将dlib编译为静态库。这是我的Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := dlib
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/entropy_encoder/entropy_encoder_kernel_2.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/entropy_encoder/entropy_encoder_kernel_1.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/all/source.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/gui_widgets/drawable.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/gui_widgets/widgets.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/gui_widgets/base_widgets.cpp
... cut many src files for simplicity.
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/threads/thread_pool_extension.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/misc_api/misc_api_kernel_1.cpp
LOCAL_SRC_FILES += $(LOCAL_PATH)/../dlib/misc_api/misc_api_kernel_2.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
include $(BUILD_STATIC_LIBRARY)
和Application.mk:
NDK_TOOLCHAIN_VERSION := clang
APP_ABI := armeabi-v7a x86
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions -Os
APP_PLATFORM := android-15
APP_STL := c++_static
#APP_CFLAGS+=-DDLIB_NO_GUI_SUPPORT=on
#APP_CFLAGS+=-DDLIB_PNG_SUPPORT=off
APP_CFLAGS+=-DDLIB_JPEG_SUPPORT=on
APP_CFLAGS+=-DDLIB_JPEG_STATIC=on
###
ANDROID_NATIVE_API_LEVEL = 15
ANDROID_NDK_HOST_X64 = true
ANDROID_SDK_TARGET = 15
这很好用。我得到了我的静态库(比使用系统gcc编译时大8倍)。
接下来我试图为JNI创建包装。我的mod.cpp:
#include <jni.h>
#include <string>
#include "dlib/image_processing/frontal_face_detector.h"
extern "C"
JNIEXPORT jstring JNICALL
Java_com_cake_appa_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
return env->NewStringUTF(hello.c_str());
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := dlib
LOCAL_SRC_FILES := $(LOCAL_PATH)/../lib/$(TARGET_ARCH_ABI)/libdlib.a
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include/
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := mod
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include/
LOCAL_C_INCLUDES += /usr/include/
LOCAL_SRC_FILES += $(LOCAL_PATH)/../mod.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_STATIC_LIBRARIES := dlib
LOCAL_STATIC_LIBRARY := dlib
include $(BUILD_SHARED_LIBRARY)
Application.mk:
NDK_TOOLCHAIN_VERSION := clang
APP_ABI := armeabi-v7a x86
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions -Os
APP_PLATFORM := android-15
APP_STL := c++_static
当我尝试构建时,我得到了什么:
cake@cake-hi:/media/storage/tmp/ndk-wrap$ export NDK_PROJECT_PATH=`pwd`
cake@cake-hi:/media/storage/tmp/ndk-wrap$ /storage/android-sdk/ndk-bundle/ndk-build
[armeabi-v7a] Compile++ thumb: mod <= mod.cpp
[armeabi-v7a] SharedLibrary : libmod.so
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1958: error: undefined reference to 'XClearArea'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1959: error: undefined reference to 'XFlush'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1984: error: undefined reference to 'XVaCreateNestedList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1985: error: undefined reference to 'XSetICValues'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1986: error: undefined reference to 'XFree'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1834: error: undefined reference to 'XAllocSizeHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1840: error: undefined reference to 'XSetNormalHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1841: error: undefined reference to 'XFree'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1844: error: undefined reference to 'XResizeWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1846: error: undefined reference to 'XFlush'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1760: error: undefined reference to 'XwcTextListToTextProperty'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1761: error: undefined reference to 'XSetWMName'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1762: error: undefined reference to 'XFree'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1763: error: undefined reference to 'XFlush'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1712: error: undefined reference to 'XDestroyWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1893: error: undefined reference to 'XTranslateCoordinates'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1868: error: undefined reference to 'XMoveWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1869: error: undefined reference to 'XFlush'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1593: error: undefined reference to 'XCreateWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1620: error: undefined reference to 'XCreateFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1623: error: undefined reference to 'XVaCreateNestedList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1624: error: undefined reference to 'XCreateIC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1631: error: undefined reference to 'XFree'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1632: error: undefined reference to 'XFreeStringList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1642: error: undefined reference to 'XGetICValues'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1644: error: undefined reference to 'XSelectInput'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1652: error: undefined reference to 'XSetWMProtocols'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1668: error: undefined reference to 'XAllocSizeHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1674: error: undefined reference to 'XSetNormalHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1690: error: undefined reference to 'XDestroyIC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1692: error: undefined reference to 'XFreeFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1777: error: undefined reference to 'XMapRaised'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1804: error: undefined reference to 'XUnmapWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:639: error: undefined reference to 'XPending'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:649: error: undefined reference to 'XNextEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:653: error: undefined reference to 'XFilterEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:679: error: undefined reference to 'XInternAtom'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:685: error: undefined reference to 'XChangeProperty'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:794: error: undefined reference to 'XwcLookupString'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:797: error: undefined reference to 'XwcLookupString'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:731: error: undefined reference to 'XAllocSizeHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:737: error: undefined reference to 'XSetNormalHints'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:745: error: undefined reference to 'XResizeWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:752: error: undefined reference to 'XMoveWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1050: error: undefined reference to 'XCheckIfEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:697: error: undefined reference to 'XChangeProperty'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:717: error: undefined reference to 'XSendEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:804: error: undefined reference to 'XLookupString'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1018: error: undefined reference to 'XDestroyWindow'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1223: error: undefined reference to 'XInitImage'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1225: error: undefined reference to 'XCreateGC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1227: error: undefined reference to 'XPutImage'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1229: error: undefined reference to 'XFreeGC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1266: error: undefined reference to 'XGetModifierMapping'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1272: error: undefined reference to 'XkbKeycodeToKeysym'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1294: error: undefined reference to 'XFreeModifiermap'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1365: error: undefined reference to 'XSetSelectionOwner'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1423: error: undefined reference to 'XInternAtom'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1424: error: undefined reference to 'XGetSelectionOwner'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1436: error: undefined reference to 'XConvertSelection'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1441: error: undefined reference to 'XPeekIfEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1444: error: undefined reference to 'XGetWindowProperty'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1461: error: undefined reference to 'XGetWindowProperty'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1468: error: undefined reference to 'XwcTextPropertyToTextList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1473: error: undefined reference to 'XwcFreeStringList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1485: error: undefined reference to 'XwcFreeStringList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:1934: error: undefined reference to 'XScreenNumberOfScreen'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:334: error: undefined reference to 'XOpenDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:337: error: undefined reference to 'XOpenDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:369: error: undefined reference to 'XFreeFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:371: error: undefined reference to 'XCreateFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:376: error: undefined reference to 'XExtentsOfFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:379: error: undefined reference to 'XFreeStringList'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:322: error: undefined reference to 'XwcTextExtents'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:388: error: undefined reference to 'XFreeGC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:389: error: undefined reference to 'XFreePixmap'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:393: error: undefined reference to 'XDefaultDepth'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:393: error: undefined reference to 'XCreatePixmap'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:394: error: undefined reference to 'XCreateGC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:398: error: undefined reference to 'XSetForeground'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:399: error: undefined reference to 'XSetBackground'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:400: error: undefined reference to 'XFillRectangle'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:401: error: undefined reference to 'XSetForeground'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:328: error: undefined reference to 'XwcDrawString'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:405: error: undefined reference to 'XGetImage'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:313: error: undefined reference to 'XAllocColor'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:435: error: undefined reference to 'XQueryColor'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:449: error: undefined reference to 'XFreeGC'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:450: error: undefined reference to 'XFreeFontSet'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:451: error: undefined reference to 'XFreePixmap'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_widgets/nativefont.h:452: error: undefined reference to 'XCloseDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:148: error: undefined reference to 'XPutBackEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:153: error: undefined reference to 'XSendEvent'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:160: error: undefined reference to 'XCloseIM'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:163: error: undefined reference to 'XCloseDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:191: error: undefined reference to 'XInitThreads'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:203: error: undefined reference to 'XOpenDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:208: error: undefined reference to 'XOpenDisplay'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:225: error: undefined reference to 'XInternAtom'
/media/storage/tmp/ndk-dlib/jni/../dlib/all/../gui_core/gui_core_kernel_2.cpp:272: error: undefined reference to 'XCreateWindow'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
/media/storage/android-sdk/ndk-bundle/build/core/build-binary.mk:677: recipe for target '/media/storage/tmp/ndk-wrap/obj/local/armeabi-v7a/libmod.so' failed
make: *** [/media/storage/tmp/ndk-wrap/obj/local/armeabi-v7a/libmod.so] Error 1
Linker说它无法找到X库。我想,我的系统包libx11-dev:i386不会做任何事情。尝试LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
导致无休止的构建,这会占用所有系统内存(6 GiB进程)。
同样的事情发生在OpenCV上,但未定义的引用与其他东西相同。
我在Debian 9.3 x86_64上。
答案 0 :(得分:0)
不要这样做;这些头文件与Android不兼容。 Android不使用glibc。LOCAL_C_INCLUDES + = / usr / include /
我想,我的系统包libx11-dev:i386不会做任何事情。
不,它不会,但你确实需要从某个地方获得你的依赖。您需要为Android构建该库并包含它(或重写您的代码以不使用该库)。