首页 > 编程知识 正文

cmake file命令,android log输出

时间:2023-05-03 14:34:54 阅读:25073 作者:3679

只要是在安卓上用过jni的人都应该知道。 所有我们编译和编写的c/c文件都必须配置用于编译的配置文件,而eclipse则需要编写mk文件。 在Android Studio中,写一个名为CMakeList.txt的配置文件。

只要编写这个配置文件,我们就可以正确编译我们想要的so动态库。 今天我们将讨论如何写Android Studio名为CMakeList.txt的文件。 eclispe的mk文件的写法在这里不叙述。 如果不知道,请在ndk的安装路径中键入doc文档,看看Android.mk的写法。 在文档中详细介绍

要知道什么叫CMakeList,你需要知道CMake是用来做什么的。 CMake是一个跨平台安装(编译)工具,它允许您用简单的语句编写所有平台的安装(编译过程)。 他可以输出各种makefile和project文件,并测试编译器(如UNIX上的automake )支持的c特性。

虽然在此不再详细使用,但我们将在ffmpeg的配置中介绍CMakeList.txt在Android Studio中的一般写法。

在此附上CMake官方学习文档:

英文文档: https://cmake.org/documentation/

中文文档: https://www.zybuluo.com/少笑松鼠-lau/note/254724

请在CMakeList.txt的开头设置jni文件夹目录,以便gcc在编译时找到相应的文件。 具体写法如下。

set (distribution _ dir $ { cmake _ source _ dir }/src/main/JNI libs ) ) ) ) ) ) ) ) ) )。

反斜线后面是固定格式,前面是你的jni文件夹所在的路径; 后面的写法一般不建议修改。

写了这个之后,中有应该导入的第三方头文件。 此时,应该配置头文件的路径。 具体写法如下。

include _ directories (src/main/JNI libs/include/ffmpeg )。

中有多个的情况下,也同样依次相加就可以了。 如果引用由第三方编译的. so库,则必须在此时进行部署和编译,如中所示。 引入第三方. so库的具体写法如下。

add_library (

视听编解码器- lib

静态

导入)

set _ target _ properties (av codec-lib

属性导入_位置

$ { cmake _ source _ dir }/src/main/JNI libs/$ { Android _ ABI }/libavcodec-56.so }

首先,要添加库(即:add_library函数),必须传递三个参数:第一个要部署的库别名,第二个库类型,静态库还是动态库。 因为Android是静态库,也就是不能引用. dll后缀的库,所以第二个参数是“SHARED”,只要引入了第三方. so库,第三个参数是用什么方法引入的?第三方参数是

在add_library之后,设置. so的详细路径。 用set_target_properties ()函数设置。 此函数也是传递三个参数以指定. so库的路径。 第一个参数与add_library中的第一个参数相同,但这里的库别名必须与add_library中的库别名匹配。 如果不匹配,则报告在编译时找不到库的错误。 第二个参数是固定的,都写为PROPERTIES IMPORTED_LOCATION,主要用于指定如何部署库。 正在当地引进。 第三个是库的具体路径。 这个不能写错。 如果写错了,在编译时也同样找不到库。 只要部署第三方库并使用add_library,它就会显示为一对,因为它使用set_target_propeties组合。

add_library还有另一种写法。 那是CMakeList.txt的默认写法

add _ library (# setsthenameofthelibrary。

播放控制器

# setsthelibraryasasharedlibrary。

共享游戏

# providesarelativepathtoyoursourcefile (s )。

src/main/cpp/playController.c )

这是编译我们自己在项目中写的抄送文件。 这里不使用set_target_propeties ()。

接下来是find_library (); 看了这个名字,我想你就会知道它是用来做什么的,用来找库的,在哪里用的。 它主要是用于查找系统库,如果项目中有系统使用的. so库,则将库名称写入此函数中以找到相应的库。

find _ library (# setsthenameofthepathvariable。

log-lib

# Specifies the name of the

NDK library that

# you want CMake to locate.

log )

比如上面用了日志库,那就要把日志库加进来。

最后一个函数target_link_libraries()这个是函数是干嘛用的呢,讲到这个函数就要讲到c/c++的编译原理了,在linux中c/c++的编译一般都是用gcc来编译的,c/c++编译时会产生.o文件要通过make工具来把这些.o文件链接起来,这样才能得一个可执行程序。所以.so在编译时要把所有库链接起来才能编译。target_link_libraries()就是干这个事的。

target_link_libraries( # Specifies the target library.

playController

avcodec-lib

avdevice-lib

avfilter-lib

avformat-lib

avutil-lib

postproc-lib

swresample-lib

swscale-lib

yuv-lib

# Links the target library to the log library

# included in the NDK.

${log-lib} )

把要链接的库别名都写到这里就可以了,如果是系统的库要用这个格式${库的名字}。

好了,CMakeList.txt的基本写法就讲到这里,如里有发现写错了,小伙伴可以留言给我,及时改正大家一起进步。

最后附上ffmpeg的CMakeList.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.

set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLib)

include_directories(src/main/cpp/include/ffmpeg)

include_directories(src/main/cpp/include/libyuv)

add_library( # Sets the name of the library.

WaynePlayer

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).

src/main/cpp/WaynePlayer.c)

add_library(

avcodec

STATIC

IMPORTED

)

set_target_properties(

avcodec

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec-56.so

)

add_library(

avdevice

STATIC

IMPORTED

)

set_target_properties(

avdevice

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavdevice-56.so

)

add_library(

avfilter

STATIC

IMPORTED

)

set_target_properties(

avfilter

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavfilter-5.so

)

add_library(

avformat

STATIC

IMPORTED

)

set_target_properties(

avformat

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavformat-56.so

)

add_library(

avutil

STATIC

IMPORTED

)

set_target_properties(

avutil

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavutil-54.so

)

add_library(

postproc

STATIC

IMPORTED

)

set_target_properties(

postproc

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpostproc-53.so

)

add_library(

swresample

STATIC

IMPORTED

)

set_target_properties(

swresample

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswresample-1.so

)

add_library(

swscale

STATIC

IMPORTED

)

set_target_properties(

swscale

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswscale-3.so

)

add_library(

yuv

STATIC

IMPORTED

)

set_target_properties(

yuv

PROPERTIES IMPORTED_LOCATION

${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libyuv.so

)

# 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.

WaynePlayer

avcodec

avdevice

avfilter

avformat

avutil

postproc

swresample

swscale

yuv

-landroid

# Links the target library to the log library

# included in the NDK.

${log-lib})

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。