NDK编译依赖opencv静态库的arm64-v8a动态库
遇到的问题:写完Android.mk和Application.mk文件,然后使用cygwin+NDK编译
总是遇到下面的编译错误:
fatal error: opencv2/core.hpp: No such file or directory
#include "opencv2/core.hpp"
在网上试了很多方法,都不奏效。
最终解决问题的办法是:将opencv和opencv2文件夹复制到这个目录:
D:\android-ndk-r17c\sysroot\usr\include
然后编译通过了。
因为引入opencv时代码是这样写的:
#include "opencv2/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/ml/ml.hpp" #include "opencv2/imgproc/imgproc.hpp"
编译器要去自己能查找的路径去搜索,很明显的搜索路径就是NDK路径,所以这个方法奏效了。
使用NDKr17c编译,其中opencv和opencv2文件夹复制到了D:\android-ndk-r17c\sysroot\usr\include文件夹下解决编译找不到文件问题。
在使用NDKr10e编译,由于没有sysroot文件夹,考虑到mk文件中配置编译器4.9和使用-std=c++11,于是找到下面这个路径,将opencv和opencv2文件夹复制到下面的路径同样解决了"opencv2/core.hpp"等opencv头文件找不到的问题。
D:\android-ndk-r10e\sources\cxx-stl\gnu-libstdc++\4.9\include
可能这不是最好的解决问题的办法,但这是一个奏效的方法。
希望可以找到更好的方法。
参考博客:https://blog.csdn.net/x397794823/article/details/51122742
下面附上mk文件:
Android.mk
LOCAL_PATH:=$(call my-dir) my_LOCAL_PATH := $(LOCAL_PATH) include $(my_LOCAL_PATH)/../../Module/ModuleAndroid64.mk LOCAL_PATH := $(my_LOCAL_PATH) include $(CLEAR_VARS) LOCAL_MODULE:=AndroidModule FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp) LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) LOCAL_LDLIBS := -llog LOCAL_SHARED_LIBRARIES := Model #LOCAL_ALLOW_UNDEFINED_SYMBOLS := true include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_PLATFORM := android-21 #APP_STL := c++_static APP_STL := gnustl_static #APP_STL := gnustl_shared APP_CPPFLAGS += -std=c++11 APP_CPPFLAGS += -frtti -fexceptions #NDK_TOOLCHAIN_VERSION = clang NDK_TOOLCHAIN_VERSION = 4.9 #当编译静态库时,可以如下这样写,那么静态库就会编译了 #APP_MODULES :=iconv APP_ABI := arm64-v8a APP_MODULES := Module AndroidModule #This optional variable can be defined to either 'release' or'debug',A 'release' mode is the default #APP_OPTIM :=debug
ModuleAndroid64.mk
LOCAL_PATH := $(call my-dir) my_LOCAL_PATH := $(LOCAL_PATH) # Include all libs (built and prebuilt) D:/OpenCV-3.4.9-android-sdk/sdk/native/jni OPENCV_PATH := D:/OpenCV-3.4.9-android-sdk/sdk/native/jni LOCAL_PATH := $(my_LOCAL_PATH) include $(CLEAR_VARS) #OpenCV_INSTALL_MODULES := on OPENCV_INSTALL_MODULES:=on OPENCV_LIB_TYPE := STATIC include $(OPENCV_PATH)/OpenCV.mk LOCAL_C_INCLUDES += \ $(OPENCV_PATH)/include LOCAL_MODULE += Module FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp) LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%) LOCAL_CFLAGS += -std=c++11 -frtti -fexceptions -fopenmp -w LOCAL_LDLIBS += -llog -lc -lm -lz -ldl include $(BUILD_SHARED_LIBRARY)