美文网首页c
使用CMake交叉编译Android环境的OpenCV

使用CMake交叉编译Android环境的OpenCV

作者: andforce | 来源:发表于2019-10-14 17:10 被阅读0次

    编译环境

    这一点十分重要,有时候如果环境不同,可能出现一些奇奇怪怪的错误,会浪费大量的时间去填坑。
    比如我用>Android NDK r18b版本编译,就报错:
    针对这个报错,我提了一个issues:https://github.com/opencv/opencv/issues/15521
    不知道以后会不会解决。
    解决了:
    如果使用>Android NDK r18b编译,需要添加下面的参数:

    -DBUILD_ZLIB=ON
    
    [ 57%] Linking CXX shared library ../../lib/armeabi-v7a/libopencv_core.so
    bionic/libc/include/bits/fortify/stdio.h:70: error: undefined reference to '__vsnprintf_chk'
    bionic/libc/include/bits/fortify/stdio.h:43: error: undefined reference to '__vsnprintf_chk'
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    modules/core/CMakeFiles/opencv_core.dir/build.make:1345: recipe for target 'lib/armeabi-v7a/libopencv_core.so' failed
    make[2]: *** [lib/armeabi-v7a/libopencv_core.so] Error 1
    CMakeFiles/Makefile2:1509: recipe for target 'modules/core/CMakeFiles/opencv_core.dir/all' failed
    make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs....
    [ 57%] Linking CXX static library ../lib/armeabi-v7a/libIlmImf.a
    [ 57%] Built target IlmImf
    Makefile:162: recipe for target 'all' failed
    make: *** [all] Error 2
    

    针对Android SDK我感觉没什么特别要求,从API Level 22 到 29我都安装了。


    image.png

    配置环境变量

    上面东西都准备好之后,我们还需要配置一下环境变量:
    在 ~/.bashrc 中添加:

    export ANDROID_HOME=/home/andforce/Android/Sdk
    export ANDROID_SDK=$ANDROID_HOME
    export ANDROID_NDK_HOME=/home/andforce/Downloads/android-ndk-r17c
    export ANDROID_NDK=$ANDROID_NDK_HOME
    

    开始编译

    解压下载的OpenCV 4.1.1源码,进入源码目录后:

    $ tree -L 1
    .
    ├── 3rdparty
    ├── apps
    ├── cmake
    ├── CMakeLists.txt
    ├── CONTRIBUTING.md
    ├── data
    ├── doc
    ├── include
    ├── LICENSE
    ├── modules
    ├── platforms
    ├── README.md
    └── samples
    
    9 directories, 4 files
    

    创建存放编译结果的目录

    $mkdir build
    

    进入build目录开始编译:

    $cd build
    # 开始执行 cmake 编译命令
    # 注意有【..】,表示源码目录在上一层,因为我们现在是在build目录
    cmake .. -DINSTALL_ANDROID_EXAMPLES=OFF \
    -DANDROID_EXAMPLES_WITH_LIBS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_DOCS=OFF \
    -DWITH_OPENCL=OFF \
    -DWITH_IPP=ON \
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
    -DANDROID_TOOLCHAIN=clang \
    -DANDROID_STL=c++_shared \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_ANDROID_PROJECTS=OFF \
    -DBUILD_ANDROID_EXAMPLES=OFF \
    -DBUILD_PREF_TESTS=OFF \
    -DBUILD_TESTS=OFF \
    -DANDROID_ABI=armeabi-v7a \
    -DANDROID_SDK_TARGET=21
    
    几个重要的参数含义
    #NDK中toolchain的路径
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake
    #toolchain是clang版本
    -DANDROID_TOOLCHAIN=clang
    #表示编译出的是.a静态库,如果要编译.so动态库,配置成c++_shared就可以了
    -DANDROID_STL=c++_static
    #不编译Android工程
    -DBUILD_ANDROID_PROJECTS=OFF
    #不编译事例工程
    -DBUILD_ANDROID_EXAMPLES=OFF
    #不编译PREF test
    -DBUILD_PREF_TESTS=OFF
    #不编译test
    -DBUILD_TESTS=OFF
    #android abi,x86,x86_64等
    -DANDROID_ABI=armeabi-v7a
    #Android target sdk
    -DANDROID_SDK_TARGET=21
    

    也许你有别的cmake参数想修改?

    首先得知道支持哪些参数,在执行完上面的cmake命令之后,在build目录执行:

    cmake -LA .
    

    可以看到,有很多很多的参数,请根据自己的情况选择吧。

    ANDROID_EXAMPLES_WITH_LIBS:BOOL=OFF
    BUILD_ANDROID_EXAMPLES:BOOL=OFF
    BUILD_ANDROID_PROJECTS:BOOL=OFF
    BUILD_ANDROID_SERVICE:BOOL=OFF
    BUILD_CUDA_STUBS:BOOL=OFF
    BUILD_DOCS:BOOL=OFF
    BUILD_EXAMPLES:BOOL=OFF
    BUILD_FAT_JAVA_LIB:BOOL=ON
    BUILD_ITT:BOOL=ON
    BUILD_JASPER:BOOL=ON
    BUILD_JAVA:BOOL=ON
    BUILD_JPEG:BOOL=ON
    BUILD_LIST:STRING=
    BUILD_OPENEXR:BOOL=ON
    BUILD_PACKAGE:BOOL=ON
    BUILD_PERF_TESTS:BOOL=ON
    BUILD_PNG:BOOL=ON
    BUILD_PROTOBUF:BOOL=ON
    BUILD_SHARED_LIBS:BOOL=OFF
    BUILD_TBB:BOOL=ON
    BUILD_TESTS:BOOL=OFF
    BUILD_TIFF:BOOL=ON
    BUILD_USE_SYMLINKS:BOOL=OFF
    BUILD_WEBP:BOOL=ON
    BUILD_WITH_DEBUG_INFO:BOOL=OFF
    BUILD_WITH_DYNAMIC_IPP:BOOL=OFF
    BUILD_ZLIB:BOOL=OFF
    BUILD_opencv_apps:BOOL=OFF
    BUILD_opencv_calib3d:BOOL=ON
    BUILD_opencv_core:BOOL=ON
    BUILD_opencv_dnn:BOOL=ON
    BUILD_opencv_features2d:BOOL=ON
    BUILD_opencv_flann:BOOL=ON
    BUILD_opencv_highgui:BOOL=ON
    BUILD_opencv_imgcodecs:BOOL=ON
    BUILD_opencv_imgproc:BOOL=ON
    BUILD_opencv_java_bindings_generator:BOOL=ON
    BUILD_opencv_js:BOOL=OFF
    BUILD_opencv_ml:BOOL=ON
    BUILD_opencv_objdetect:BOOL=ON
    BUILD_opencv_photo:BOOL=ON
    BUILD_opencv_stitching:BOOL=ON
    BUILD_opencv_ts:BOOL=ON
    BUILD_opencv_video:BOOL=ON
    BUILD_opencv_videoio:BOOL=ON
    BUILD_opencv_world:BOOL=OFF
    CAROTENE_NS:STRING=carotene_o4t
    CCACHE_PROGRAM:FILEPATH=CCACHE_PROGRAM-NOTFOUND
    CMAKE_AR:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar
    CMAKE_ASM_FLAGS:STRING=
    CMAKE_ASM_FLAGS_DEBUG:STRING=
    CMAKE_ASM_FLAGS_RELEASE:STRING=
    CMAKE_BUILD_TYPE:STRING=
    CMAKE_COLOR_MAKEFILE:BOOL=ON
    CMAKE_CONFIGURATION_TYPES:STRING=Debug;Release
    CMAKE_CXX_COMPILER_AR:FILEPATH=CMAKE_CXX_COMPILER_AR-NOTFOUND
    CMAKE_CXX_COMPILER_RANLIB:FILEPATH=CMAKE_CXX_COMPILER_RANLIB-NOTFOUND
    CMAKE_CXX_FLAGS:STRING=
    CMAKE_CXX_FLAGS_DEBUG:STRING=
    CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
    CMAKE_CXX_FLAGS_RELEASE:STRING=
    CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
    CMAKE_CXX_STANDARD_LIBRARIES:STRING=-latomic -lm "/home/andforce/Downloads/android-ndk-r17c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "/home/andforce/Downloads/android-ndk-r17c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++abi.a" "/home/andforce/Downloads/android-ndk-r17c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libandroid_support.a" "/home/andforce/Downloads/android-ndk-r17c/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a" "-ldl"
    CMAKE_C_COMPILER_AR:FILEPATH=CMAKE_C_COMPILER_AR-NOTFOUND
    CMAKE_C_COMPILER_RANLIB:FILEPATH=CMAKE_C_COMPILER_RANLIB-NOTFOUND
    CMAKE_C_FLAGS:STRING=
    CMAKE_C_FLAGS_DEBUG:STRING=
    CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
    CMAKE_C_FLAGS_RELEASE:STRING=
    CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG
    CMAKE_C_STANDARD_LIBRARIES:STRING=-latomic -lm
    CMAKE_EXE_LINKER_FLAGS:STRING=
    CMAKE_EXE_LINKER_FLAGS_DEBUG:STRING=
    CMAKE_EXE_LINKER_FLAGS_MINSIZEREL:STRING=
    CMAKE_EXE_LINKER_FLAGS_RELEASE:STRING=
    CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
    CMAKE_EXPORT_COMPILE_COMMANDS:BOOL=OFF
    CMAKE_INSTALL_PREFIX:PATH=/home/andforce/Downloads/opencv-4.1.1/build/install
    CMAKE_LINKER:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld
    CMAKE_MAKE_PROGRAM:FILEPATH=/usr/bin/make
    CMAKE_MODULE_LINKER_FLAGS:STRING=
    CMAKE_MODULE_LINKER_FLAGS_DEBUG:STRING=
    CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL:STRING=
    CMAKE_MODULE_LINKER_FLAGS_RELEASE:STRING=
    CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO:STRING=
    CMAKE_NM:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-nm
    CMAKE_OBJCOPY:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-objcopy
    CMAKE_OBJDUMP:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-objdump
    CMAKE_RANLIB:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ranlib
    CMAKE_SHARED_LINKER_FLAGS:STRING=
    CMAKE_SHARED_LINKER_FLAGS_DEBUG:STRING=
    CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL:STRING=
    CMAKE_SHARED_LINKER_FLAGS_RELEASE:STRING=
    CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO:STRING=
    CMAKE_SKIP_INSTALL_RPATH:BOOL=NO
    CMAKE_SKIP_RPATH:BOOL=NO
    CMAKE_STATIC_LINKER_FLAGS:STRING=
    CMAKE_STATIC_LINKER_FLAGS_DEBUG:STRING=
    CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL:STRING=
    CMAKE_STATIC_LINKER_FLAGS_RELEASE:STRING=
    CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO:STRING=
    CMAKE_STRIP:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-strip
    CMAKE_TOOLCHAIN_FILE:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/build/cmake/android.toolchain.cmake
    CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE
    CPACK_BINARY_DEB:BOOL=OFF
    CPACK_BINARY_FREEBSD:BOOL=OFF
    CPACK_BINARY_IFW:BOOL=OFF
    CPACK_BINARY_NSIS:BOOL=OFF
    CPACK_BINARY_RPM:BOOL=OFF
    CPACK_BINARY_STGZ:BOOL=ON
    CPACK_BINARY_TBZ2:BOOL=OFF
    CPACK_BINARY_TGZ:BOOL=ON
    CPACK_BINARY_TXZ:BOOL=OFF
    CPACK_BINARY_TZ:BOOL=ON
    CPACK_SOURCE_RPM:BOOL=OFF
    CPACK_SOURCE_TBZ2:BOOL=ON
    CPACK_SOURCE_TGZ:BOOL=ON
    CPACK_SOURCE_TXZ:BOOL=ON
    CPACK_SOURCE_TZ:BOOL=ON
    CPACK_SOURCE_ZIP:BOOL=OFF
    CPUFEATURES_ROOT:PATH=/home/andforce/Downloads/opencv-4.1.1/3rdparty/cpufeatures
    CPU_BASELINE:STRING=DETECT
    CPU_DISPATCH:STRING=
    CV_DISABLE_OPTIMIZATION:BOOL=OFF
    CV_ENABLE_INTRINSICS:BOOL=ON
    CV_TRACE:BOOL=ON
    ENABLE_BUILD_HARDENING:BOOL=OFF
    ENABLE_CCACHE:BOOL=ON
    ENABLE_CONFIG_VERIFICATION:BOOL=OFF
    ENABLE_FAST_MATH:BOOL=OFF
    ENABLE_IMPL_COLLECTION:BOOL=OFF
    ENABLE_INSTRUMENTATION:BOOL=OFF
    ENABLE_NOISY_WARNINGS:BOOL=OFF
    ENABLE_PIC:BOOL=ON
    ENABLE_PRECOMPILED_HEADERS:BOOL=OFF
    ENABLE_SOLUTION_FOLDERS:BOOL=OFF
    ENABLE_THIN_LTO:BOOL=OFF
    EXECUTABLE_OUTPUT_PATH:PATH=/home/andforce/Downloads/opencv-4.1.1/build/bin
    GENERATE_ABI_DESCRIPTOR:BOOL=OFF
    GIT_EXECUTABLE:FILEPATH=/usr/bin/git
    HAVE_IEEEFP:STRING=1
    HOST_BIG_ENDIAN:STRING=OFF
    HOST_FILLORDER:STRING=FILLORDER_LSB2MSB
    INSTALL_ANDROID_EXAMPLES:BOOL=OFF
    INSTALL_CREATE_DISTRIB:BOOL=OFF
    INSTALL_C_EXAMPLES:BOOL=OFF
    INSTALL_PYTHON_EXAMPLES:BOOL=OFF
    INSTALL_TESTS:BOOL=OFF
    LIBRARY_OUTPUT_PATH_ROOT:PATH=/home/andforce/Downloads/opencv-4.1.1/build
    M_LIBRARY:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/platforms/android-14/arch-arm/usr/lib/libm.so
    OPENCV_CONFIG_FILE_INCLUDE_DIR:PATH=/home/andforce/Downloads/opencv-4.1.1/build
    OPENCV_DNN_CUDA:BOOL=OFF
    OPENCV_DNN_OPENCL:BOOL=OFF
    OPENCV_DOWNLOAD_PATH:PATH=/home/andforce/Downloads/opencv-4.1.1/.cache
    OPENCV_DUMP_HOOKS_FLOW:BOOL=OFF
    OPENCV_ENABLE_MEMORY_SANITIZER:BOOL=OFF
    OPENCV_ENABLE_NONFREE:BOOL=OFF
    OPENCV_EXTRA_MODULES_PATH:PATH=
    OPENCV_FORCE_3RDPARTY_BUILD:BOOL=OFF
    OPENCV_GENERATE_PKGCONFIG:BOOL=OFF
    OPENCV_MATHJAX_RELPATH:STRING=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0
    OPENCV_PYTHON3_VERSION:BOOL=OFF
    OPENCV_TIMESTAMP:STRING=2019-09-13T14:41:40Z
    OPENCV_WARNINGS_ARE_ERRORS:BOOL=OFF
    OpenCV_HAL_DIR:PATH=OpenCV_HAL_DIR-NOTFOUND
    PROTOBUF_UPDATE_FILES:BOOL=OFF
    PYTHON2_EXECUTABLE:FILEPATH=/usr/bin/python2.7
    PYTHON2_INCLUDE_DIR:PATH=
    PYTHON2_INCLUDE_DIR2:PATH=
    PYTHON2_LIBRARY:FILEPATH=
    PYTHON2_LIBRARY_DEBUG:FILEPATH=
    PYTHON2_NUMPY_INCLUDE_DIRS:PATH=
    PYTHON2_PACKAGES_PATH:PATH=
    PYTHON3_EXECUTABLE:FILEPATH=/usr/bin/python3
    PYTHON3_INCLUDE_DIR:PATH=
    PYTHON3_INCLUDE_DIR2:PATH=
    PYTHON3_LIBRARY:FILEPATH=
    PYTHON3_LIBRARY_DEBUG:FILEPATH=
    PYTHON3_NUMPY_INCLUDE_DIRS:PATH=
    PYTHON3_PACKAGES_PATH:PATH=
    USE_WIN32_FILEIO:BOOL=FALSE
    VIDEOIO_ENABLE_PLUGINS:BOOL=ON
    VIDEOIO_ENABLE_STRICT_PLUGIN_CHECK:BOOL=ON
    VIDEOIO_PLUGIN_LIST:STRING=
    WITH_ADE:BOOL=ON
    WITH_ANDROID_MEDIANDK:BOOL=OFF
    WITH_ARITH_DEC:BOOL=ON
    WITH_ARITH_ENC:BOOL=ON
    WITH_CAROTENE:BOOL=ON
    WITH_CLP:BOOL=OFF
    WITH_CPUFEATURES:BOOL=ON
    WITH_CUDA:BOOL=OFF
    WITH_EIGEN:BOOL=OFF
    WITH_FFMPEG:BOOL=OFF
    WITH_GDCM:BOOL=OFF
    WITH_HALIDE:BOOL=OFF
    WITH_HPX:BOOL=OFF
    WITH_IMGCODEC_HDR:BOOL=ON
    WITH_IMGCODEC_PFM:BOOL=ON
    WITH_IMGCODEC_PXM:BOOL=ON
    WITH_IMGCODEC_SUNRASTER:BOOL=ON
    WITH_INF_ENGINE:BOOL=OFF
    WITH_ITT:BOOL=ON
    WITH_JASPER:BOOL=ON
    WITH_JPEG:BOOL=ON
    WITH_LIBREALSENSE:BOOL=OFF
    WITH_OPENCL:BOOL=OFF
    WITH_OPENCL_SVM:BOOL=OFF
    WITH_OPENEXR:BOOL=ON
    WITH_OPENMP:BOOL=OFF
    WITH_OPENVX:BOOL=OFF
    WITH_PNG:BOOL=ON
    WITH_PROTOBUF:BOOL=ON
    WITH_PTHREADS_PF:BOOL=ON
    WITH_QUIRC:BOOL=ON
    WITH_TBB:BOOL=OFF
    WITH_TIFF:BOOL=ON
    WITH_VULKAN:BOOL=OFF
    WITH_WEBP:BOOL=ON
    ZLIB_INCLUDE_DIR:PATH=/home/andforce/Downloads/android-ndk-r17c/sysroot/usr/include
    ZLIB_LIBRARY_DEBUG:FILEPATH=ZLIB_LIBRARY_DEBUG-NOTFOUND
    ZLIB_LIBRARY_RELEASE:FILEPATH=/home/andforce/Downloads/android-ndk-r17c/platforms/android-14/arch-arm/usr/lib/libz.so
    ccitt:BOOL=ON
    logluv:BOOL=ON
    lzw:BOOL=ON
    mdi:BOOL=ON
    next:BOOL=ON
    old-jpeg:BOOL=OFF
    opencv_dnn_PERF_CAFFE:BOOL=OFF
    opencv_dnn_PERF_CLCAFFE:BOOL=OFF
    packbits:BOOL=ON
    thunder:BOOL=ON
    

    回到正题,继续说如何编译

    cmake结束之后,输出类似下面的信息,表示成功了

    -- Install to: /home/andforce/Downloads/opencv-4.1.1/build/install


    --
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/andforce/Downloads/opencv->4.1.1/build

    看一下build目录下生成的东西:

    build$ tree -L 1
    .
    ├── 3rdparty
    ├── bin
    ├── carotene
    ├── CMakeCache.txt
    ├── CMakeDownloadLog.txt
    ├── CMakeFiles
    ├── cmake_install.cmake
    ├── cmake_uninstall.cmake
    ├── CMakeVars.txt
    ├── configured
    ├── CPackConfig.cmake
    ├── CPackSourceConfig.cmake
    ├── CTestTestfile.cmake
    ├── custom_hal.hpp
    ├── cvconfig.h
    ├── cv_cpu_config.h
    ├── data
    ├── doc
    ├── include
    ├── lib
    ├── Makefile                # <-- 生成了Makefile
    ├── modules
    ├── opencv2
    ├── OpenCV-armeabi-v7a.mk
    ├── OpenCVConfig.cmake
    ├── OpenCVConfig-version.cmake
    ├── opencv_data_config.hpp
    ├── OpenCV.mk
    ├── OpenCVModules.cmake
    ├── opencv_tests_config.hpp
    ├── platforms
    ├── unix-install
    └── version_string.tmp
    
    13 directories, 20 files
    

    还是在build目录,执行make -j8开始编译

    #根据电脑性能可以调整-j后面的参数,一般为CPU核心数的2倍
    $make -j8
    

    Scanning dependencies of target libcpufeatures
    [ 0%] Building C object 3rdparty/cpufeatures/CMakeFiles/libcpufeatures.dir/cpu-features.c.o
    [ 0%] Building C object 3rdparty/libjasper/CMakeFiles/libjasper.dir/jas_cm.c.o
    .
    .此处省略...
    .
    [ 74%] Building CXX object >modules/ts/CMakeFiles/opencv_ts.dir/src/cuda_test.cpp.o
    .
    .此处省略...
    .
    [ 99%] Building CXX object modules/imgproc/CMakeFiles/opencv_perf_imgproc.dir/perf/perf_warp.cpp.o
    [100%] Linking CXX executable ../../bin/opencv_perf_imgproc
    [100%] Built target opencv_perf_imgproc

    跑到100%表示编译结束了

    在build/lib/armeabi-v7a生成了对应的静态库

    build/lib/armeabi-v7a$ ls
    libopencv_calib3d.so     libopencv_highgui.so    libopencv_photo.so
    libopencv_core.so        libopencv_imgcodecs.so  libopencv_stitching.so
    libopencv_dnn.so         libopencv_imgproc.so    libopencv_ts.a
    libopencv_features2d.so  libopencv_ml.so         libopencv_videoio.so
    libopencv_flann.so       libopencv_objdetect.so  libopencv_video.so
    
    

    生成sdk

    还是在build目录下,执行make install

    build$ make install
    

    之后会在build/install/下生成sdk

    build/install$ tree -L 3
    .
    ├── LICENSE
    ├── README.android
    └── sdk
        ├── etc
        │   ├── haarcascades
        │   ├── lbpcascades
        │   ├── licenses
        │   ├── valgrind_3rdparty.supp
        │   └── valgrind.supp
        └── native
            ├── 3rdparty
            ├── jni
            └── staticlibs
    
    9 directories, 4 files
    

    头文件在:build/install/sdk/native/jni/include/

    build/install/sdk/native/jni/include/opencv2$ ls
    calib3d      dnn.hpp         highgui.hpp    ml.hpp              photo.hpp      videoio.hpp
    calib3d.hpp  features2d      imgcodecs      objdetect           stitching
    core         features2d.hpp  imgcodecs.hpp  objdetect.hpp       stitching.hpp
    core.hpp     flann           imgproc        opencv.hpp          video
    cvconfig.h   flann.hpp       imgproc.hpp    opencv_modules.hpp  video.hpp
    dnn          highgui         ml             photo               videoio
    

    静态库在:build/install/sdk/native/staticlibs/

    build/install/sdk/native/staticlibs/armeabi-v7a$ ls 
    libopencv_calib3d.so     libopencv_highgui.so    libopencv_photo.so
    libopencv_core.so        libopencv_imgcodecs.so  libopencv_stitching.so
    libopencv_dnn.so         libopencv_imgproc.so    libopencv_videoio.so
    libopencv_features2d.so  libopencv_ml.so         libopencv_video.so
    libopencv_flann.so       libopencv_objdetect.so
    

    到此,整个交叉编译过程就结束了。

    cmake .. \
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DANDROID_ABI="x86" \
    -DANDROID_NATIVE_API_LEVEL=21 \
    -DWITH_CAROTENE=OFF \
    -DWITH_CLP=OFF \
    -DWITH_CUBLAS=OFF \
    -DWITH_CUDA=OFF \
    -DWITH_CUFFT=OFF \
    -DWITH_EIGEN=OFF \
    -DWITH_GDCM=OFF \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_JASPER=OFF \
    -DWITH_JPEG=OFF \
    -DWITH_NVCUVID=OFF \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENCL_SVM=OFF \
    -DWITH_OPENEXR=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_OPENVX=OFF \
    -DWITH_PNG=ON \
    -DWITH_PTHREADS_PF=OFF \
    -DWITH_TBB=OFF \
    -DWITH_TIFF=OFF \
    -DWITH_WEBP=OFF \
    -DBUILD_ANDROID_EXAMPLES=OFF \
    -DBUILD_ANDROID_SERVICE=OFF \
    -DBUILD_CUDA_STUBS=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_FAT_JAVA_LIB=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_OPENEXR=OFF \
    -DBUILD_PACKAGE=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_PNG=ON \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_WITH_DYNAMIC_IPP=OFF \
    -DBUILD_opencv_apps=OFF \
    -DBUILD_opencv_calib3d=ON \
    -DBUILD_ZLIB=ON \
    -DBUILD_opencv_core=ON \
    -DBUILD_opencv_features2d=ON \
    -DBUILD_opencv_flann=ON \
    -DBUILD_opencv_highgui=ON \
    -DBUILD_opencv_imgcodecs=ON \
    -DBUILD_opencv_imgproc=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_ml=ON \
    -DBUILD_opencv_objdetect=OFF \
    -DBUILD_opencv_photo=OFF \
    -DBUILD_opencv_shape=OFF \
    -DBUILD_opencv_stitching=OFF \
    -DBUILD_opencv_stereo=OFF \
    -DBUILD_opencv_superres=OFF \
    -DBUILD_opencv_ts=OFF \
    -DBUILD_opencv_video=OFF \
    -DBUILD_opencv_videoio=OFF \
    -DBUILD_opencv_line_descriptor=OFF \
    -DBUILD_opencv_reg=OFF \
    -DBUILD_opencv_saliency=OFF \
    -DBUILD_opencv_videostab=OFF \
    -DBUILD_opencv_world=OFF \
    -DCMAKE_CXX_FLAGS="-ffunction-sections -fdata-sections -fvisibility=hidden -O3 -std=c++11 -mfloat-abi=softfp -mfpu=neon -march=armv7-a -mtune=cortex-a8" \
    -DCMAKE_C_FLAGS="-ffunction-sections -fdata-sections -fvisibility=hidden -O3 -mfloat-abi=softfp -mfpu=neon -march=armv7-a -mtune=cortex-a8" \
    -DCMAKE_SHARED_LINKER_FLAGS="-Wl,--gc-sections" \
    -DBUILD_opencv_xfeatures2d=OFF \
    -DBUILD_opencv_face=OFF \
    -DBUILD_opencv_bgsegm=OFF \
    -DBUILD_opencv_datasets=OFF \
    -DBUILD_opencv_dpm=OFF \
    -DBUILD_opencv_tracking=OFF \
    -DBUILD_opencv_xobjdetect=OFF \
    -DBUILD_opencv_optflow=OFF \
    -DBUILD_opencv_tracking=OFF \
    -DENABLE_NEON=ON \
    -DBUILD_opencv_ximgproc=ON \
    -DBUILD_opencv_dnn=OFF \
    -DBUILD_opencv_structured_light=OFF \
    -DBUILD_opencv_surface_matching=OFF \
    -DBUILD_opencv_text=OFF \
    -DBUILD_opencv_xphoto=OFF \
    -DBUILD_opencv_fuzzy=OFF \
    -DBUILD_opencv_bioinspired=OFF \
    -DBUILD_opencv_phase_unwrapping=OFF \
    -DBUILD_opencv_plot=OFF \
    -DBUILD_opencv_rgbd=OFF \
    -DBUILD_opencv_aruco=OFF
    
    
    

    相关文章

      网友评论

        本文标题:使用CMake交叉编译Android环境的OpenCV

        本文链接:https://www.haomeiwen.com/subject/qnxoyctx.html