Visual Studio + android + opencv 跨平台生成动态库文件

谷梁波
2023-12-01

最近需要给安卓平台的应用程序提供c++开发的算法库,c++又用到了opencv开源库,不想使用Android Studio,用惯了VS,因此,写下VS跨平台开发安卓动态库的配置步骤。

 

参考链接:https://answers.opencv.org/question/179297/how-to-build-opencv-for-android-using-visual-studio/

https://blog.csdn.net/luoyu510183/article/details/94590497

 

官方说明文档(用于跨平台移动开发的 Visual C++):https://docs.microsoft.com/zh-cn/visualstudio/cross-platform/visual-cpp-for-cross-platform-mobile-development?view=vs-2017

(使用 C++ 安装跨平台移动开发)https://docs.microsoft.com/zh-cn/visualstudio/cross-platform/install-visual-cpp-for-cross-platform-mobile-development?view=vs-2017

 

 

第一步更改VS

选择更多-修改-工作负载-移动与游戏-使用c++的移动开发

选择安装,等待安装完成。

 

第二步配置VS:Setting up Visual Studio

  1. Create a new Visual Studio Project from "Visual C++/Cross Platform/Android" and choose the type of Project you want.
  2. Add the path "Path_to_build_folder/install/sdk/native/jni/include" to "Additional Include directories" and both the "native/3rdparty/libs/armeabi-v7a/" and "native/libs/armeabi-v7a/" to "Additional Library Directories".
  3. In your projects property dialog change "Enable C++ Exceptions" to "Yes (-fexceptions)" under "C/C++ - Code Generation" and "Enable Run-Time Type Information" to "Yes (-frtti)" as well. You can find it under "C/C++ - Language". Also add "-std=c++11" as Additional Option in the Command Line Section of "C/C++".
  4. In the Linker section of your property page, add "-lm -lz" to the command line additional options. This ensures correct linking.
  5. Under "Linker/Input", add all your built modules and also the 3rdparty libs as "Library Dependencies". Important: Only their names, e.g. not "libopencv_core.a", but "opencv_core". The linker will figure out the rest by itself. Now comes my favourite part: As UNIX-style linkers always scan libraries from right to left, the order in which you add the libraries matters. Now you have two options: Either play around with the order of the libraries until you figure out the correct one, or (as found by me after searching for literally hours), just go to your OpenCV build directory and search for the file "OpenCV.mk". In there the line "OPENCV_MODULES" indicates the correct order of the modules. The 3rdparty libraries have to be added after the modules in the Library Dependencies field.

 

第三步,调用例子

1.新建项目-c++-跨平台-android,选择Dynamaic Shared Library(Android)

2. In "Solution platform" :Change "Debug x86" to "Release ARM".

3. Follow FERNman's answer do step 2,3,4. By the way,i don't have "libs" dir,use "staticlibs" dir replace.

4. Under "Linker/Input",Be careful of "Library Dependencies",the lib file is made up of three parts:"lib"+"name"+".a",there only need "name":e.g.Transform "libopencv_world.a" into "opencv_world" manually and type it on "Library Dependencies"

 

编译后,出错(原因是没有区分清楚库依赖项和附加依赖项):

1>------ 已启动全部重新生成: 项目: SharedObject, 配置: Release ARM ------

1>ANDROID_HOME=C:\ProgramData\Microsoft\AndroidSDK\25

1>ANT_HOME=C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Apps\apache-ant-1.9.3

1>JAVA_HOME=C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25

1>NDK_ROOT=C:\\Microsoft\AndroidNDK64\android-ndk-r15c

1>pch.h

1>SharedObject.cpp

1>clang.exe: error: no such file or directory: 'opencv_dnn'

1>clang.exe: error: no such file or directory: 'opencv_highgui'

1>clang.exe: error: no such file or directory: 'opencv_ml'

1>clang.exe: error: no such file or directory: 'opencv_objdetect'

1>clang.exe: error: no such file or directory: 'opencv_photo'

1>clang.exe: error: no such file or directory: 'opencv_stitching'

1>clang.exe: error: no such file or directory: 'opencv_video'

1>clang.exe: error: no such file or directory: 'opencv_calib3d'

1>clang.exe: error: no such file or directory: 'opencv_features2d'

1>clang.exe: error: no such file or directory: 'opencv_flann'

1>clang.exe: error: no such file or directory: 'opencv_videoio'

1>clang.exe: error: no such file or directory: 'opencv_imgcodecs'

1>clang.exe: error: no such file or directory: 'opencv_imgproc'

1>clang.exe: error: no such file or directory: 'opencv_core'

1>clang.exe: error: no such file or directory: 'cpufeatures'

1>clang.exe: error: no such file or directory: 'IlmImf'

1>clang.exe: error: no such file or directory: 'ittnotify'

1>clang.exe: error: no such file or directory: 'libjasper'

1>clang.exe: error: no such file or directory: 'libjpeg-turbo'

1>clang.exe: error: no such file or directory: 'libpng'

1>clang.exe: error: no such file or directory: 'libprotobuf'

1>clang.exe: error: no such file or directory: 'tiff'

1>clang.exe: error: no such file or directory: 'webp'

1>clang.exe: error: no such file or directory: 'quirc'

1>clang.exe: error: no such file or directory: 'tbb'

1>clang.exe: error: no such file or directory: 'tegra_hal'

1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Application Type\Android\3.0\Android.Common.targets(104,5): error MSB6006: “clang.exe”已退出,代码为 1。

1>已完成生成项目“SharedObject.vcxproj”的操作 - 失败。

 

参考资料:https://blog.csdn.net/rrrfff/article/details/51112230

工具-选项-跨平台,修改sdk,ndk等相关地址

 

又出错:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCTargets\Microsoft.Cpp.Clang.targets(152,5): error MSB6006: “clang.exe”已退出,代码为 5。

VS2017无解,改用VS2019 不配置opencv-android-sdk时,编译OK

 

给VS2019配置opencv-android-sdk, 编译出错:

>NDK_ROOT=F:\android\android-ndk-r16-beta1

clang.exe: error: no such file or directory: 'opencv_dnn'

...

Android.Common.targets(119,5): error MSB6006: “clang.exe”已退出,代码为 1。

解决方法:

>NDK_ROOT=F:\android\android-ndk-r10e

出错:

>TRACKER : ���� TRK0005: δ���ҵ�: ��clang.exe����ϵͳ�Ҳ���ָ�����ļ���

Microsoft.Cpp.Clang.targets(154,5): error MSB6006: “clang.exe”已退出,代码为 5。

 

经过探索,VS2019 c++-跨平台-android的Dynamaic Shared Library(Android)按照以下步骤配置,可以编译通过:

1. 工具-选项-跨平台,修改sdk,ndk等相关地址

NDK_ROOT=F:\android\android-ndk-r16-beta1

2. 解决方案——属性——配置属性——常规:

平台工具集:Clang5.0

目标API级别:android-19 ----> android21 opencv相关函数才能调用成功

具体可以参考我的下一篇文章:VS跨平台编译安卓动态库

https://mp.csdn.net/postedit/102517851

STL的使用:LLVM libc++ 静态库(c++_static)

3. 解决方案——右击——限定此范围

4. 编译,成功

5. 配置opencv-android-sdk静态库,除附加依赖项外,其他照常配置,编译,成功

6. 添加库依赖项,编译,成功

opencv_dnn

opencv_highgui

opencv_ml

opencv_objdetect

opencv_photo

opencv_stitching

opencv_video

opencv_calib3d

opencv_features2d

opencv_flann

opencv_videoio

opencv_imgcodecs

opencv_imgproc

opencv_core

cpufeatures

IlmImf

ittnotify

libjasper

libjpeg-turbo

libpng

libprotobuf

libtiff

libwebp

quirc

tbb

tegra_hal

或者修改附加依赖项:

相对地址方式,编译,成功

-lopencv_dnn

-lopencv_highgui

-lopencv_ml

-lopencv_objdetect

-lopencv_photo

-lopencv_stitching

-lopencv_video

-lopencv_calib3d

-lopencv_features2d

-lopencv_flann

-lopencv_videoio

-lopencv_imgcodecs

-lopencv_imgproc

-lopencv_core

-lcpufeatures

-lIlmImf

-littnotify

-llibjasper

-llibjpeg-turbo

-llibpng

-llibprotobuf

-llibtiff

-llibwebp

-lquirc

-ltbb

-ltegra_hal

绝对地址方式,编译成功

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_dnn.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_highgui.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_ml.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_objdetect.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_photo.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_stitching.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_video.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_calib3d.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_features2d.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_flann.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_videoio.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_imgcodecs.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_imgproc.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\staticlibs\armeabi-v7a\libopencv_core.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libcpufeatures.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libIlmImf.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libittnotify.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibjasper.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibjpeg-turbo.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibpng.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibprotobuf.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibtiff.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\liblibwebp.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libquirc.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libtbb.a

E:\opencv-4.1.1-android-sdk\OpenCV-android-sdk\sdk\native\3rdparty\libs\armeabi-v7a\libtegra_hal.a

 

 

 

 

 类似资料: