Hanjie's Blog

一只有理想的羊驼

使用Android studio在Ubuntu中编译某个安卓项目时,发现当NDK版本为16时会出现以下错误:

1
2
3
4
5
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':libuvccamera:ndkClean'.
> Process 'command '/home/luohanjie/Android/Sdk/ndk/android-ndk-r16b/ndk-build'' finished with non-zero exit value 2

而NDK版本改为20或者21时,则没有问题。

而由于所使用的部分库是使用NDK16版本进行编译的,所以需要对库使用NDK21进行重新编译,但是编译时发生以下问题:

1
2
3
4
CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0x90): undefined reference to `cv::Feature2D::detect(cv::_InputArray const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&, cv::_InputArray const&)'
CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0x98): undefined reference to `cv::Feature2D::detect(cv::_InputArray const&, std::__ndk1::vector<std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >, std::__ndk1::allocator<std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> > > >&, cv::_InputArray const&)'
CMakeFiles/allencv.dir/detect/HRB.cpp.o:(.data.rel.ro+0xa0): undefined reference to `cv::Feature2D::compute(cv::_InputArray const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&, cv::_OutputArray const&)'
...

最终发现原因是NDK与OpenCV Android发生冲突1。新版本的NDK改用libc++作为默认的STL,而OpenCV Android 3.4版本使用的是gnustl。可以升级到OpenCV 4.0版本来兼容新版本的NDK,或者可以使用libc++重新编译OpenCV:

1
2
3
4
5
6
cd $OPENCV_SRC
mkdir build
cd build
export ANDROID_NDK=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21
export ANDROID_SDK_ROOT=/home/luohanjie/Android/Sdk
cmake -DCMAKE_TOOLCHAIN_FILE=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21/build/cmake/android.toolchain.cmake -DANDROID_ABI="arm64-v8a" -D -DANDROID_STL=c++_static -DCMAKE_BUILD_TYPE=Release -DANDROID_NATIVE_API_LEVEL=android-27 -DANDROID_PLATFORM=27 -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_opencv_java=ON -DBUILD_FAT_JAVA_LIB=ON -DBUILD_ANDROID_PROJECTS=ON -DJAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_AWT_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jawt.h -DJAVA_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_INCLUDE_PATH2=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux -DJAVA_JVM_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jni.h -DANT_EXECUTABLE=/usr/bin/ant -DOPENCV_EXTRA_MODULES_PATH=/home/luohanjie/Documents/software/opencv-3.4.11/opencv_contrib/modules ..

注意arm64-v8a不能使用-D ENABLE_VFPV3=ON。而且我并没有使用TBB,-D WITH_TBB=ON,为何我发现使用TBB反而更慢。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- General configuration for OpenCV 3.4.11 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/luohanjie/Documents/software/opencv-3.4.11/opencv_contrib/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2020-09-24T09:27:47Z
-- Host: Linux 4.15.0-99-generic x86_64
-- Target: Android 1 aarch64
-- CMake: 3.12.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: NEON FP16
-- required: NEON
-- disabled: VFPV3
--
-- C/C++:
-- Built as dynamic libs?: NO
-- C++11: YES
-- C++ Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ (ver 9.0)
-- C++ flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG
-- C Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- C flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O2 -DNDEBUG -DNDEBUG
-- C flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a dl m log
-- 3rdparty dependencies: libcpufeatures ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc tegra_hal
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc java line_descriptor ml objdetect optflow phase_unwrapping photo plot reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf js matlab ovis python2 python3 sfm ts viz
-- Applications: -
-- Documentation: NO
-- Non-free algorithms: NO
--
-- Android NDK: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21 (ver 21.0.6113669)
-- Android ABI: arm64-v8a
-- NDK toolchain: aarch64-linux-android-clang
-- STL type: c++_static
-- Native API level: 27
-- Android SDK: /home/luohanjie/Android/Sdk (tools: 24.4.1 build tools: 30.0.0)
-- android tool: /home/luohanjie/Android/Sdk/tools/android
-- SDK target: android-27
-- Projects build scripts: Ant/Eclipse compatible
--
-- GUI:
--
-- Media I/O:
-- ZLib: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libz.a (ver 1.2.7)
-- JPEG: build-libjpeg-turbo (ver 2.0.5-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: build (ver 1.6.37)
-- TIFF: build (ver 42 - 4.0.10)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- Python (for build): /usr/bin/python2.7
--
-- Java: export all functions
-- ant: /usr/bin/ant (ver 1.9.6)
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /home/luohanjie/Documents/software/opencv-3.4.11/build/install
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/luohanjie/Documents/software/opencv-3.4.11/build
1
2
make -j4
make install

如果是想编译32位版本:

1
2
3
4
5
6
cd $OPENCV_SRC
mkdir build
cd build
export ANDROID_NDK=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21
export ANDROID_SDK_ROOT=/home/luohanjie/Android/Sdk
cmake -DCMAKE_TOOLCHAIN_FILE=/home/luohanjie/Android/Sdk/ndk/android-ndk-r21/build/cmake/android.toolchain.cmake -DANDROID_ABI="armeabi-v7a with NEON" -D ENABLE_VFPV3=ON -DANDROID_STL=c++_static -DCMAKE_BUILD_TYPE=Release -DANDROID_NATIVE_API_LEVEL=android-27 -DANDROID_PLATFORM=27 -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_ANDROID_EXAMPLES=OFF -DBUILD_opencv_java=ON -DBUILD_FAT_JAVA_LIB=ON -DBUILD_ANDROID_PROJECTS=ON -DJAVA_AWT_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_AWT_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jawt.h -DJAVA_INCLUDE_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -DJAVA_INCLUDE_PATH2=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux -DJAVA_JVM_LIBRARY=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/jni.h -DANT_EXECUTABLE=/usr/bin/ant -DOPENCV_EXTRA_MODULES_PATH=/home/luohanjie/Documents/software/opencv-3.4.11/opencv_contrib/modules ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- General configuration for OpenCV 3.4.11 =====================================
-- Version control: unknown
--
-- Extra modules:
-- Location (extra): /home/luohanjie/Documents/software/opencv-3.4.11/opencv_contrib/modules
-- Version control (extra): unknown
--
-- Platform:
-- Timestamp: 2020-09-24T09:37:21Z
-- Host: Linux 4.15.0-99-generic x86_64
-- Target: Android 1 armv7-a
-- CMake: 3.12.3
-- CMake generator: Unix Makefiles
-- CMake build tool: /usr/bin/make
-- Configuration: Release
--
-- CPU/HW features:
-- Baseline: VFPV3 NEON
-- requested: DETECT
-- required: VFPV3 NEON
--
-- C/C++:
-- Built as dynamic libs?: NO
-- C++11: YES
-- C++ Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ (ver 9.0)
-- C++ flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -Oz -DNDEBUG -DNDEBUG
-- C++ flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG
-- C Compiler: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- C flags (Release): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -Oz -DNDEBUG -DNDEBUG
-- C flags (Debug): -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Winconsistent-missing-override -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Qunused-arguments -mfpu=neon -fvisibility=hidden -fvisibility-inlines-hidden -O0 -fno-limit-debug-info -DDEBUG -D_DEBUG
-- Linker flags (Release): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- Linker flags (Debug): -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,--as-needed
-- ccache: YES
-- Precompiled headers: NO
-- Extra dependencies: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libz.a dl m log
-- 3rdparty dependencies: libcpufeatures ittnotify libprotobuf libjpeg-turbo libwebp libpng libtiff libjasper IlmImf quirc tegra_hal
--
-- OpenCV modules:
-- To be built: aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy hfs highgui img_hash imgcodecs imgproc java line_descriptor ml objdetect optflow phase_unwrapping photo plot reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
-- Disabled: world
-- Disabled by dependency: -
-- Unavailable: cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv freetype hdf js matlab ovis python2 python3 sfm ts viz
-- Applications: -
-- Documentation: NO
-- Non-free algorithms: NO
--
-- Android NDK: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21 (ver 21.0.6113669)
-- Android ABI: armeabi-v7a
-- NDK toolchain: arm-linux-androideabi-clang
-- STL type: c++_static
-- Native API level: 27
-- Android SDK: /home/luohanjie/Android/Sdk (tools: 24.4.1 build tools: 30.0.0)
-- android tool: /home/luohanjie/Android/Sdk/tools/android
-- SDK target: android-27
-- Projects build scripts: Ant/Eclipse compatible
--
-- GUI:
--
-- Media I/O:
-- ZLib: /home/luohanjie/Android/Sdk/ndk/android-ndk-r21/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libz.a (ver 1.2.7)
-- JPEG: build-libjpeg-turbo (ver 2.0.5-62)
-- WEBP: build (ver encoder: 0x020f)
-- PNG: build (ver 1.6.37)
-- TIFF: build (ver 42 - 4.0.10)
-- JPEG 2000: build (ver 1.900.1)
-- OpenEXR: build (ver 2.3.0)
-- HDR: YES
-- SUNRASTER: YES
-- PXM: YES
--
-- Video I/O:
--
-- Parallel framework: pthreads
--
-- Trace: YES (with Intel ITT)
--
-- Other third-party libraries:
-- Custom HAL: YES (carotene (ver 0.0.1))
-- Protobuf: build (3.5.1)
--
-- Python (for build): /usr/bin/python2.7
--
-- Java: export all functions
-- ant: /usr/bin/ant (ver 1.9.6)
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /home/luohanjie/Documents/software/opencv-3.4.11/build/install
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/luohanjie/Documents/software/opencv-3.4.11/build
0%