如何使用 CMake 在 android 构建中编译和共享两个 C++ 库
How to compile and share two c++ libraries on an android build using CMake
我有自定义 C++ 类,我想 运行 在我的 android 应用程序上。我使用 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.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\ndk0~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 文件夹,并将其作为附加包含目录传递给 cmake:
https://cmake.org/cmake/help/v3.0/command/include_directories.html
示例:
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.com/ahasbini/Android-OpenCV/blob/master/app/CMakeLists.txt
您的 cmake 项目似乎找不到已安装的 opencv,因此您要么将路径设置为 ${complete_path_to_opencv_installation}/apt
,要么将 opencv 安装在 公共安装文件夹
无论哪种方式,您都需要将其传播到您的项目中:
如果您将其安装在典型位置(例如 linux 中的 /usr/local/
,您需要在 Windows 中设置 export PATH=$PATH:${opencv_installation_path}
或 c:\Path
,然后设置 路径在环境变量.
通过这样做,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} )
或者您也可以在 include_directories
之前 set OpenCV_INCLUDE_DIRS "./app/opencv2"
传播 opencv2/core.hpp
.
您也可能对此 将 opencv 与 android 结合使用感兴趣。
我有自定义 C++ 类,我想 运行 在我的 android 应用程序上。我使用 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.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\ndk0~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 文件夹,并将其作为附加包含目录传递给 cmake:
https://cmake.org/cmake/help/v3.0/command/include_directories.html
示例:
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.com/ahasbini/Android-OpenCV/blob/master/app/CMakeLists.txt
您的 cmake 项目似乎找不到已安装的 opencv,因此您要么将路径设置为 ${complete_path_to_opencv_installation}/apt
,要么将 opencv 安装在 公共安装文件夹
无论哪种方式,您都需要将其传播到您的项目中:
如果您将其安装在典型位置(例如 linux 中的 /usr/local/
,您需要在 Windows 中设置 export PATH=$PATH:${opencv_installation_path}
或 c:\Path
,然后设置 路径在环境变量.
通过这样做,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} )
或者您也可以在 include_directories
之前 set OpenCV_INCLUDE_DIRS "./app/opencv2"
传播 opencv2/core.hpp
.
您也可能对此