Windows编译OpenCV4Android解决undefined reference to std错误
Posted 2523984508
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows编译OpenCV4Android解决undefined reference to std错误相关的知识,希望对你有一定的参考价值。
注意OpenCV 4.0.1 解决了这个问题请直接下载OpenCV 4.0.1
但是OpenCV 4.0.1作为模块导入android Studio会有找不到R.styleable的问题
OpenCV4Android支持三种使用方式
1.Java层调用
2.NDK调用动态库(方便,但是会包含整个.so库)
3.NDK调用静态库(灵活,比如没有用到机器学习模块,libopencv_ml.a里的代码是不会打包到动态库里的)
通过链接静态库的方式使用OpenCV4Android,CMAKE配置如下
#############################prepare for using of opencv static library#################### #first step to use opencv set(OpenCV_STATIC ON) #second step to use opencv set(OpenCV_DIR C:/sdk/opencv-3.4.2-android-sdk/sdk/native/jni) #third step to use opencv find_package(OpenCV REQUIRED) ########################################################################################### ##################################################the native-lib shared library############ # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib #foutth step to use opencv link opencv ${OpenCV_LIBS} # Links the target library to the log library # included in the NDK. ${log-lib}) ######################################################################################
仅仅是新建项目稍稍修改了native-lib.cpp的代码,在函数里用了下opencv的函数
#include <jni.h> #include <string> #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc_c.h" extern "C" JNIEXPORT jstring JNICALL Java_com_example_useprebuiltstaticlibrary_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello="hello"; cv::Mat m=cv::Mat::eye(3,3,CV_8UC1); return env->NewStringUTF(hello.c_str()); }
编译时却报错
Build command failed. Error while executing process C:\\sdk\\cmake\\3.6.4111459\\bin\\cmake.exe with arguments {--build C:\\sdk\\demo\\usePrebuiltStaticLibrary\\app\\.externalNativeBuild\\cmake\\debug\\armeabi-v7a --target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o [2/2] Linking CXX shared library ..\\..\\..\\..\\build\\intermediates\\cmake\\debug\\obj\\armeabi-v7a\\libnative-lib.so FAILED: cmd.exe /C "cd . && C:\\sdk\\ndk-bundle\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\clang++.exe --target=armv7-none-linux-androideabi23 --gcc-toolchain=C:/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=C:/sdk/ndk-bundle/sysroot -fPIC -isystem C:/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot C:/sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -LC:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -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 C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_highgui.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_features2d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_shape.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgcodecs.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_ml.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videoio.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_dnn.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_flann.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_objdetect.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_calib3d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_video.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_superres.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_photo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgproc.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_stitching.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videostab.a -llog C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibprotobuf.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_calib3d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_features2d.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_highgui.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_videoio.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgcodecs.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjpeg-turbo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibwebp.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibpng.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibtiff.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/liblibjasper.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libIlmImf.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_flann.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_video.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_photo.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_imgproc.a C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a -lz C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libtbb.a -lc C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libcpufeatures.a -ldl -lm -llog C:/sdk/opencv-3.4.2-android-sdk/sdk/native/3rdparty/libs/armeabi-v7a/libtegra_hal.a -latomic -lm "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_static.a" "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++abi.a" "C:/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libunwind.a" "-ldl" && cd ." C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) [clone .part.13]: error: undefined reference to \'std::basic_ios<char, std::char_traits<char> >::clear(std::_Ios_Iostate)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::ostream::put(char)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::ostream::flush()\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(system.cpp.o):system.cpp:function cv::Exception::formatMessage(): error: undefined reference to \'std::ostream::put(char)\'
.
.
.
.
.
.
.
.
.
C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(persistence_json.cpp.o):persistence_json.cpp:function icvJSONParseValue(CvFileStorage*, char*, CvFileNode*): error: undefined reference to \'std::string::at(unsigned int)\' C:/sdk/opencv-3.4.2-android-sdk/sdk/native/staticlibs/armeabi-v7a/libopencv_core.a(persistence.cpp.o):persistence.cpp:function check_if_write_struct_is_delayed(CvFileStorage*, bool): error: undefined reference to \'std::string::assign(char const*, unsigned int)\' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
google得知是因为编译这些静态库时所用的编译器、标准库等跟NDK当前的版本不同。网上的建议都是stl改为gnustl_static或gnustl_shared之类。可较新的NDK都不支持这些stl了,为了使用OpenCV静态库只能重新编译了。
编译OpenCV4Android的过程里爬了不少坑,总结一下,希望有助于小白们。
1. 安装MinGW
这里不详细的说了,网上很多教程。选择如图三项安装
安装好后差不多二三百兆,在环境变量Path里添加
C:\\MinGW\\bin
C:\\MinGW\\msys\\1.0\\bin
这里C:\\MinGW是我的安装目录。配置好后需要注销计算机,再登陆。在命令行里gcc -v看到能输出版本就算差不多设置好环境了。
2. CMAKE 配置
下载好源代码解压,我放在C:\\opencv-3.4.2。打开CMAKE图形界面
如图一定要选择MinGW Makefiles,我想这也是为什么一定要先安装配置MinGW的原因。选择指定工具链,点击next
这里一定要注意,选择的不是源代码
\\opencv-3.4.2\\platforms\\android\\android.toolchain.cmake
这个工具链,这个工具链过时了。新版本的NDK带有自己的工具链,这里选择NDK
/build/cmake/android.toolchain.cmake这个工具链。
这里感谢
https://github.com/opencv/opencv/issues/8742
点击Finish。有错误别管,点击configure。开始配置。出现错误就再点configure直到遇到一个跳不过去的错误,看看是不是让你配置SDK路径
Android SDK: specify path to Android SDK via ANDROID_SDK_ROOT /
ANDROID_HOME / ANDROID_SDK variables
点击Add Entry添加变量
注意类型是FilePath。点击OK。点击configure。很快又跳出一个错误
CMake Error at cmake/android/android_gradle_projects.cmake:1 (message): Android gradle-based build/projects are not supported in this version of OpenCV. You need to downgrade Android SDK Tools to version 25.2.5. Details: https://github.com/opencv/opencv/issues/8460 Call Stack (most recent call first): cmake/android/OpenCVDetectAndroidSDK.cmake:204 (include) CMakeLists.txt:645 (include)
这是SDK比较新,新的SDK只支持Gradle编译,而OpenCV需要Ant编译。这里也给出了解决方法
Details: https://github.com/opencv/opencv/issues/8460
就是下载一个较旧的SDK。正如连接所说,windows选择这个
https://dl.google.com/android/repository/tools_r25.2.5-windows.zip
解压后发现里面只有一个tool目录,而SDK里也有一个tool目录。我的做法是先重命名SDK里的tool为tool28。再将下载的tool复制进SDK。
点击configure。(这里可能还是这个错误,这样就需要删除C:\\opencv-3.4.2\\build里所有内容,重启CMAKE。再来一次。所以这一步最好在CMAKE前就做好。
接下来会遇到这个错误
CMake Error at cmake/android/OpenCVDetectAndroidSDK.cmake:184 (message): Android SDK: Can\'t build Android projects as requested by BUILD_ANDROID_PROJECTS=ON variable. Use BUILD_ANDROID_PROJECTS=OFF to prepare Android project files without building them Call Stack (most recent call first): CMakeLists.txt:645 (include)
如图搜索这个变量,取消选择。点击configure。
配置成功。
如图搜做tbb选择两项,否则不生成libtbb.a库,我不知这又什么用,但是预编译的SDK里有这个。
还有其它的一些变量可以配置,比如添加ANDROID_ABI,否则仅仅生成armeabi-v7a。修改变量、添加变量后记得configure。
配置完成点击generate,生成完成。CMAKE可关闭了。
3.编译
命令行进入C:\\opencv-3.4.2\\build目录,这里有很多配置好的文件,输入mingw32-make,就开始编译。等好久就成功了。
C:\\opencv-3.4.2\\build>mingw32-make Scanning dependencies of target gen-pkgconfig [ 0%] Generate opencv.pc [ 0%] Built target gen-pkgconfig Scanning dependencies of target libcpufeatures [ 0%] Building C object 3rdparty/cpufeatures/CMakeFiles/libcpufeatures.dir/cpu-features.c.o [ 0%] Linking C static library ..\\lib\\armeabi-v7a\\libcpufeatures.a [ 0%] Built target libcpufeatures Scanning dependencies of target libjpeg-turbo [ 0%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapimin.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcapistd.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccoefct.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jccolor.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcdctmgr.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jchuff.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcinit.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmainct.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmarker.c.o [ 1%] Building C object 3rdparty/libjpeg-turbo/CMakeFiles/libjpeg-turbo.dir/src/jcmaster.c.o . . . . . . . . . [ 99%] Linking CXX executable ..\\..\\bin\\opencv_perf_stitching [ 99%] Built target opencv_perf_stitching Scanning dependencies of target opencv_videostab [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/deblurring.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/fast_marching.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/frame_source.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/global_motion.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/inpainting.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/log.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/motion_stabilizing.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/optical_flow.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/outlier_rejection.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/stabilizer.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_videostab.dir/src/wobble_suppression.cpp.o [100%] Linking CXX static library ..\\..\\lib\\armeabi-v7a\\libopencv_videostab.a [100%] Built target opencv_videostab Scanning dependencies of target opencv_test_videostab [100%] Building CXX object modules/videostab/CMakeFiles/opencv_test_videostab.dir/test/test_main.cpp.o [100%] Building CXX object modules/videostab/CMakeFiles/opencv_test_videostab.dir/test/test_motion_estimation.cpp.o [100%] Linking CXX executable ..\\..\\bin\\opencv_test_videostab [100%] Built target opencv_test_videostab
4.配置库
生成的库在
\\opencv-3.4.2\\build\\lib
第三方库在
\\opencv-3.4.2\\build\\3rdparty\\lib
将其复制到
\\opencv-3.4.2-android-sdk
相应目录下替换掉原来的
现在编译项目则没有链接错误了。
参考文章:
1.https://github.com/opencv/opencv/issues/8742
2.https://github.com/opencv/opencv/issues/8460
3.https://blog.csdn.net/xiao19950820/article/details/77119891
以上是关于Windows编译OpenCV4Android解决undefined reference to std错误的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV4Android:non-system libraries in linker flags: -lopencv_java错误的解决