我有自定义的C++类,我想在我的android应用上运行。我已经成功地使用 CMakeLists
. 但由于我的类使用opencv,我得到了一个很明显的问题
fatal error: 'opencv2/core.hpp' file not found
然后我试着在我的android应用上添加opencv库,下载了open cv android sdk并累加到我的项目上。以下是文件夹结构
android
-> app
-> opencv2
All the opencv2 c++ files and folders
->folder1
My custom c++ classes which will import opencv2.
以下是我的CMakeLists.txt文件。
cmake_minimum_required(VERSION 3.4.1) # for example
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
PROJECT(tag)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_library( tag_native
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
"./folder1/tag_native.cpp" )
add_library( opencv2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
"./opencv2/" )
set_target_properties(opencv2 PROPERTIES LINKER_LANGUAGE CXX)
这是我的cpp文件的例子
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <opencv2/core.hpp>
从c++编译的所有东西都很好,但我还是得到同样的问题。
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process C:\Users\Brainants with arguments {Technology\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe -C E:\Squtag\Squtag Mobile\android\app\.cxx\cmake\debug\armeabi-v7a opencv2 squtag_native}
ninja: Entering directory `E:\Squtag\Squtag Mobile\android\app\.cxx\cmake\debug\armeabi-v7a'
[1/2] Building CXX object CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o
FAILED: CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o
C:\Users\BRAINA~1\AppData\Local\Android\Sdk\ndk\210~1.611\TOOLCH~1\llvm\prebuilt\WINDOW~1\bin\CLANG_~1.EXE --target=armv7-none-linux-androideabi24 --gcc-toolchain="C:/Users/Brainants Technology/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64" --sysroot="C:/Users/Brainants Technology/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot" -Dsqutag_native_EXPORTS -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -Wformat -Werror=format-security -fsanitize=address -fno-omit-frame-pointer -std=c++11 -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o -MF CMakeFiles\squtag_native.dir\E_\Squtag\Squtag_Mobile\ios\Runner\Squtag_Native\squtag_native.cpp.o.d -o CMakeFiles/squtag_native.dir/E_/Squtag/Squtag_Mobile/ios/Runner/Squtag_Native/squtag_native.cpp.o -c "E:/Squtag/Squtag Mobile/ios/Runner/Squtag Native/squtag_native.cpp"
E:/Squtag/Squtag Mobile/ios/Runner/Squtag Native/squtag_native.cpp:6:10: fatal error: 'opencv2/core.hpp' file not found
#include <opencv2/core.hpp>
^~~~~~~~~~~~~~~~~~
1 error generated.
ninja: build stopped: subcommand failed.
你得到的错误是告诉你它找不到opencv的包含文件。
#include <opencv2/core.hpp>
你需要找到opencv2文件夹,并把它作为一个额外的include目录传递给cmake。
https:/cmake.orgcmakehelpv3.0commandinclude_directories.html。
例子: 在Android Studio上通过CMake将OpenCV添加到Native C代码中。
cmake_minimum_required(VERSION 3.4.1)
set(OpenCV_DIR "src/sdk/native/jni")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
或者你可以使用这种方法。https:/github.comahasbiniAndroid-OpenCVblobmasterappCMakeLists.txt。
看起来你的 cmake 项目找不到安装的 opencv,所以要么你把路径设置成了 ${complete_path_to_opencv_installation}/apt
或者你把opencv安装在 通用安装文件夹
无论哪种方式,你都需要将其传播到你的项目中。如果你把它安装在典型的地方(例如 /usr/local/
在linux中,你需要设置 export PATH=$PATH:${opencv_installation_path}
或 c:\Path
然后在Windows中设置 路径 在 环境变量.
这样做,就能使 find_package(OpenCV)
应该可以找到opencv库和 OpenCV_INCLUDE_DIRS
设置.这 CMakeLists.txt 应该可以。
cmake_minimum_required(VERSION 3)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
或者你也可以 set OpenCV_INCLUDE_DIRS "./app/opencv2"
之前 include_directories
传播 opencv2/core.hpp
.
你也可以对这个感兴趣 旧职 关于在Android中使用opencv。