【发布时间】:2020-09-26 07:46:36
【问题描述】:
我有想要在我的 android 应用程序上运行的自定义 C++ 类。我已使用CMakeLists 成功绑定了我的 C++ 文件。但是由于我的课程使用 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.
【问题讨论】:
-
忘记链接中的指南。这完全是关于在 Java 中使用 opencv。这很简单,就像您使用 CMakeLists 处理 C++ 文件一样。 1. 为 Android 设备构建 OpenCV 库 2. 将库和头文件复制到您的 Android 项目中,并将其与 CMakeLists.txt 链接
-
我也试过了,但是我的导入系统不起作用。基本上我必须通过排除库名称来更改重要声明。我想知道的是如何将现有的 c++ 代码绑定到新库并在我原来的 c++ 文件中使用它。
-
这个问题似乎更可能是关于将库与 CMake 链接。如果这是一个问题,那么它是否是 Android 并不重要,因为您成功地将主 C++ 绑定到 Android 项目。我不太擅长 CMake,所以你为什么不尝试使用 CMake 搜索链接库或使用 CMake 标签重新询问?
-
如果您使用
CMake链接库,则必须先构建它。构建 OpenCV 库以制作.so或.a库。然后,您可以链接库和标题。看看sisik.eu/blog/android/ndk/opencv-without-java这个。 -
我的意思是一个库,是一个内置的C++
.so或.a库,而不是整个OpenCV库的源代码
标签: c++ opencv cmake android-ndk