为 android ndk 构建 dlib

Building dlib for android ndk

我正在尝试为 android-ndk

构建 dlib

我下载了 dlib 库并编写了 Android.mkApplication.mk 文件。

Android.mk

LOCAL_PATH := $(call my-dir)

# Define the shared library module used by the calling JNI C++ module
include $(CLEAR_VARS)
LOCAL_MODULE := dlib
LOCAL_C_INCLUDES := $(LOCAL_PATH)/dlib
LOCAL_SRC_FILES += \
        ../$(LOCAL_PATH)/dlib/dlib/threads/threads_kernel_shared.cpp \
        ../$(LOCAL_PATH)/dlib/dlib/entropy_decoder/entropy_decoder_kernel_2.cpp \
        ../$(LOCAL_PATH)/dlib/dlib/base64/base64_kernel_1.cpp \
        ../$(LOCAL_PATH)/dlib/dlib/threads/threads_kernel_1.cpp \
        ../$(LOCAL_PATH)/dlib/dlib/threads/threads_kernel_2.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)

include $(BUILD_STATIC_LIBRARY)

TOP_LEVEL_PATH := $(abspath $(LOCAL_PATH)/..)
$(info TOP Level Path : $(TOP_LEVEL_PATH))
EXT_INSTALL_PATH = $(TOP_LEVEL_PATH)/third_party
OPENCV_PATH =  $(EXT_INSTALL_PATH)/opencv/jni
OPENCV_INCLUDE_DIR = $(OPENCV_PATH)/include

include $(CLEAR_VARS)
OpenCV_INSTALL_MODULES := on
OpenCV_CAMERA_MODULES := off
OPENCV_LIB_TYPE := static
include $(OPENCV_PATH)/OpenCV.mk

LOCAL_MODULE := dlib-lib
LOCAL_C_INCLUDES += \
                $(OPENCV_INCLUDE_DIR)

LOCAL_DLIBS := -lm -llog -ldl -lz -lnigraphics
LOCAL_CPP_FLAGS += -frtti -fexceptions -std=c++11

LOCAL_STATIC_LIBRARIES += dlib

include $(BUILD_SHARED_LIBRARY)

Application.mk

NDK_TOOLCHAIN_VERSION := clang
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 mips64 mips armeabi
APP_CPPFLAGS := -std=c++11 -frtti -fexceptions
APP_PLATFORM := android-8
APP_STL := gnustl_static

我能够构建库,然后将库复制到我的 jniLibs 文件夹中。我将生成的库包含在我的 CMakeList.txt in Android-Studio.

CMakeList.txt

add_library( lib_dlib SHARED IMPORTED )

set_target_properties(lib_dlib PROPERTIES IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libdlib-lib.so")

target_link_libraries(native-lib ${log-lib} lib_dlib)

include_directories("${CMAKE_SOURCE_DIR}/generateLibs/jni/dlib")

我已经从 android-studio 中的本机 cpp 程序调用了 dlib 中的函数。

但是我得到以下错误。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':mobile:externalNativeBuildDebug'.
> Build command failed.
  Error while executing '/opt/android-sdk-linux/cmake/3.6.3155560/bin/cmake' with arguments {--build mobile/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
  [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
  Android clang version 3.8.256229  (based on LLVM 3.8.256229)
  Target: armv7-none-linux-android
  Thread model: posix
  InstalledDir: /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
  Found candidate GCC installation: /opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
  Selected GCC installation: /opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
  Candidate multilib: .;@m32
  Selected multilib: .;@m32
   "/opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++" -cc1 -triple thumbv7-none-linux-android -S -disable-free -disable-llvm-verifier -main-file-name native-lib.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -no-integrated-as -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu cortex-a8 -target-feature +soft-float-abi -target-feature -fp-only-sp -target-feature +d16 -target-feature +vfp3 -target-feature -fp16 -target-feature -vfp4 -target-feature -fp-armv8 -target-feature -neon -target-feature -crypto -target-abi aapcs-linux -mfloat-abi soft -target-linker-version 2.24 -v -v -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -ffunction-sections -coverage-file mobile/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -resource-dir /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/clang/3.8.256229 -dependency-file CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o.d -sys-header-deps -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -isystem /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -isystem /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -D native_lib_EXPORTS -I ../../../../generateLibs/jni/dlib -D ANDROID -D ANDROID -I /usr/local/include -isysroot /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm -internal-isystem /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/local/include -internal-isystem /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/clang/3.8.256229/include -internal-externc-isystem /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/include -internal-externc-isystem /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/include -O0 -Wformat -Werror=format-security -Wformat -Werror=format-security -Wall -Werror -Wall -Werror -std=gnu++11 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir mobile/.externalNativeBuild/cmake/debug/armeabi-v7a -ferror-limit 19 -fmessage-length 0 -femulated-tls -stack-protector 2 -fallow-half-arguments-and-returns -fno-signed-char -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o /tmp/native-lib-070222.s -x c++ mobile/src/main/cpp/native-lib.cpp
  clang -cc1 version 3.8.256229 based upon LLVM 3.8.256229 default target x86_64-unknown-linux
  ignoring nonexistent directory "/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/local/include"
  ignoring nonexistent directory "/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/include"
  #include "..." search starts here:
  #include <...> search starts here:
   ../../../../generateLibs/jni/dlib
   /usr/local/include
   /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include
   /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include
   /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward
   /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/clang/3.8.256229/include
   /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/include
  End of search list.
   "/opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/as" -mfpu=neon -mfloat-abi=softfp -march=armv7-a -mfpu=vfpv3-d16 -I ../../../../generateLibs/jni/dlib -I /usr/local/include --noexecstack --noexecstack -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o /tmp/native-lib-070222.s
  [2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
  FAILED: : && /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++  -target armv7-none-linux-androideabi -gcc-toolchain /opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -std=c++11 -frtti -fexceptions -v -Wall -Werror -std=c++11 -frtti -fexceptions -v -Wall -Werror -I/usr/local/include -std=gnu++11 -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib/liblog.so ../../../../src/main/jniLibs/armeabi-v7a/libopencv_java3.so ../../../../src/main/jniLibs/armeabi-v7a/libdlib-lib.so -lm "/opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a" && :
  Android clang version 3.8.256229  (based on LLVM 3.8.256229)
  Target: armv7-none-linux-android
  Thread model: posix
  InstalledDir: /opt/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
  Found candidate GCC installation: /opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
  Selected GCC installation: /opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
  Candidate multilib: .;@m32
  Selected multilib: .;@m32
   "/opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm -X --eh-frame-hdr -m armelf_linux_eabi -shared -dynamic-linker /system/bin/linker -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib/../lib/crtbegin_so.o -L/opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x -L/opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib -L/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib/../lib -L/opt/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib -L/opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib --build-id --warn-shared-textrel --fatal-warnings --fix-cortex-a8 --no-undefined -z noexecstack -z relro -z now --build-id --warn-shared-textrel --fatal-warnings --fix-cortex-a8 --no-undefined -z noexecstack -z relro -z now -soname libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib/liblog.so ../../../../src/main/jniLibs/armeabi-v7a/libopencv_java3.so ../../../../src/main/jniLibs/armeabi-v7a/libdlib-lib.so -lm /opt/android-sdk-linux/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a -lstdc++ -lm -lgcc -ldl -lc -lgcc -ldl /opt/android-sdk-linux/ndk-bundle/platforms/android-19/arch-arm/usr/lib/../lib/crtend_so.o
  ../../../../generateLibs/jni/dlib/dlib/threads/threads_kernel_shared.h:44: error: undefined reference to 'USER_ERROR__missing_dlib_all_source_cpp_file__OR__inconsistent_use_of_DEBUG_or_ENABLE_ASSERTS_preprocessor_directives_'
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

LOCAL_STATIC_LIBRARIES += dlib

您可能想要 LOCAL_WHOLE_STATIC_LIBRARIES += dlibLOCAL_STATIC_LIBRARIES 不会重新导出整个库。