https://www.imooc.com/video/20907
在已有项目中开发ndk
应用的build.gradle文件修改
android {
defaultConfig {
//...
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt" //路径
}
}
}
defaultConfig cmake 解析
cppFlags
arguments "-DANDROID_TOOLCHAIN=clang"
abiFilters "armeabi-v7a", "armeabi","x86"
cppFlags "-std=c++11 -frtti -fexceptions"
arguments "-DOpenCV_DIR=" + opencvsdk + "/sdk/native/jni" // , "-DANDROID_ARM_NEON=TRUE"
新建项目默认的 CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
详解 CMakeLists.txt
1、message
set(var 1111) # 设置变量 第一个 变量名 第二个参数 变量值
message(WARNING ${var}) # ${变量名} 取出变量值 WARNING 打印级别
#[message 不打印的问题 https://www.jianshu.com/p/80482e90eb33](https://www.jianshu.com/p/80482e90eb33)
gradle 任务 externalNativeBuildDebug 可打印显示
image.png
在不修改CMakeLists.txt文件时再次执行是不会打印出message的,此时可以删除这两件文件夹
image.png
message(WARNING log-lib ${log-lib})
log-lib 所指定的文件路径
D:/Android/android-ndk-r16b/platforms/android-21/arch-x86_64/usr/lib64/liblog.so
内置变量
file方法
查找文件
file(GLOB srcs *.cpp *.c) # 查找当前目录下的.cpp .c 文件保存在srcs变量中,是绝对路径
file(GLOB srcs *.cpp *.c aa/*.cpp) #若有查找指定目录下的带路径 如aa/*.cpp
file(GLOB hdrs *.hpp *.h)
add_library(demo ${srcs } ${hdrs }) #查找到的文件用于library
aux_source_directory(. srcs) # 搜索当前目录下的所有.cpp文件存到变量srcs中,是aux_source_directory返回的是相对路径
aux_source_directory(aa srcs) # 搜索aa目录下的所有.cpp文件
find_package
find_package(OpenCV)
message(WARNING "OpenCV=${OpenCV_FOUND},${OpenCV_INCLUDE_DIRS}")
if(OpenCV_FOUND)
message(WARNING ${OpenCV_INCLUDE_DIRS})
else()
message(WARNING OpenCV not Found)
endif()
输出如下
debug|x86_64 :-- Found OpenCV: C:/Users/Administrator/Desktop/OpenCV-android-sdk (found version "4.2.0") found components: opencv_calib3d opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs opencv_imgproc opencv_ml opencv_objdetect opencv_photo opencv_stitching opencv_video opencv_videoio
debug|x86_64 :CMake Warning at CMakeLists.txt:22 (message):
debug|x86_64 : OpenCV=1,C:/Users/Administrator/Desktop/OpenCV-android-sdk/sdk/native/jni/include
debug|x86_64 :CMake Warning at CMakeLists.txt:23 (message):
debug|x86_64 : C:/Users/Administrator/Desktop/OpenCV-android-sdk/sdk/native/jni/include
set(ANDROID_OPENCV_COMPONENTS "opencv_java" CACHE STRING "")
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
#从OpenCV 中查找opencv_java库
#REQUIRED表示如果没有找到,cmake会停止处理,并报告一个错误.
find_package(OpenCV REQUIRED COMPONENTS ${ANDROID_OPENCV_COMPONENTS})
message(WARNING "OpenCV=${OpenCV_FOUND},${OpenCV_INCLUDE_DIRS}")
find_library
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
log-lib 变量名 log要查找的库
package 与library的区别
Python入门之面向对象module,library,package之间区别
package 中包含多个library
include_directories
参考include_directories和find_package
是用来提供找头文件路径的,打个比方,我现在想要#include"cv.h"
,但是这个cv.h的路径是/usr/local/include/opencv
,那么我总不能在主函数头前写
#include “/usr/local/include/opencv/cv.h”
吧,这个时候就用到include_directories
了,它提供了一个搜索头文件暂时的根目录,即你可以在cmakelists中写上
include_directories(/usr/local/include)
来让库文件搜索以/usr/local/include
为基础,即在main函数前写上#include “opencv/cv.h"
即可
include_directories(aa)
image.png
c++ 11标准支持
set( CMAKE_CXX_FLAGS "-std=c++11" )
//添加c++ 11标准支持
网友评论