cmake编译学习

学习目标:

cmake编译学习

学习内容:

gardle 中配置cmake
默认情况下,cmake 会输出 4 种 ABI(“armeabi-v7a” , “arm64-v8a”, “x86”, “x86_64”),如下所示:

在这里插入图片描述
我们可以在其中任意一个版本下会找到,build_command.txt,这个就是构建命令

//cmake的可执行文件在那个路径 (绝对路径)
Executable : D:\AndroidStudio\sdk\cmake\3.10.2.4988404\bin\cmake.exe

arguments : 
//编译的源码放在哪个文件夹
-HD:\AndroidProjects\DymaicImageGif\app\src\main\cpp
//临时目录
-DCMAKE_FIND_ROOT_PATH=D:\AndroidProjects\DymaicImageGif\app\.cxx\cmake\debug\prefab\arm64-v8a\prefab
//编译模式
-DCMAKE_BUILD_TYPE=Debug
//导入系统的库 
-DCMAKE_TOOLCHAIN_FILE=D:\AndroidStudio\sdk\ndk\21.1.6352462\build\cmake\android.toolchain.cmake
//编译平台
-DANDROID_ABI=arm64-v8a
//NDK绝对路径
-DANDROID_NDK=D:\AndroidStudio\sdk\ndk\21.1.6352462
//Android最低平台版本
-DANDROID_PLATFORM=android-19
//当前编译 生成so的版本
-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a
//Debug下的NDK绝对路径
-DCMAKE_ANDROID_NDK=D:\AndroidStudio\sdk\ndk\21.1.6352462
//Debug下打开Cmake命令输出
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
//指定临时中间文件
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\AndroidProjects\DymaicImageGif\app\build\intermediates\cmake\debug\obj\arm64-v8a
//指定语法解释器 ninja
-DCMAKE_MAKE_PROGRAM=D:\AndroidStudio\sdk\cmake\3.10.2.4988404\bin\ninja.exe
//android 系统名称  Android
-DCMAKE_SYSTEM_NAME=Android
//系统版本
-DCMAKE_SYSTEM_VERSION=19
//最终生成的so库所在的地方
-BD:\AndroidProjects\DymaicImageGif\app\.cxx\cmake\debug\arm64-v8a
-GNinja
jvmArgs : 

Build command args:

CMakeLists.txt(配置文件使用)

//cmake 不区分大小写
# 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最小版本声明
cmake_minimum_required(VERSION 3.10.2)
//编译器打印日志
message("log first")
//内置变量 打印工程目录
message(${工程名+_SOURCE_DIR})
message(${PROJECT_SOURCE_DIR})
//打印编译目录
message(${PROJRCT_BINARY_DIR})

//导入同级目录lib文件下的所有文件
include_directories(
	lib
)

//默认生成静态库
add_library(工程目录 STATIC 文件名)
add_library(工程目录 SHARED 文件名)

//定义一个变量 staticField类型时静态的 (导入库) 动态库:STATIC 换成 SHARED
add_library(staticField STATIC IMPORTED)
//导入添加工程目录的lib目录下的exam.a静态库
set_property(TARGET staticField PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/exam.a)
//导入静态库后进行链接
target_link_libraries(
	工程目录名称 staticField
)

//设置生成library输出目录
set(LIBRARY_OUT_PATH ${PROJECT_SOURCE_DIR}/lib)
//申明一个变量temp = main.c
set(temp main.c)
# Declares and names the project.

project("dymaicimagegif")

# 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

             dgif_lib.c

             gifalloc.c


        )

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

find_library(
        jnigraphics-lib
        jnigraphics
        )

# 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}
                       ${jnigraphics-lib} )

C++程序编译过程
在这里插入图片描述
当我们执行完链接后就生成了可执行文件,会发生打包库的操作。

静态链接库

Linux下静态库命名规则(类似模块化,编译时必须存在)
必须是lib{your_library_name}.a:lib为前缀,your_library_name是静态库名,扩展名为.a
所谓静态库,是因为在链接阶段,会将汇编生成的目标文件.o与引用到的库一起链接打包到可执行文件中,对应的链接方式成为静态链接。

  • 优点
    代码装载速度快,程序在运行时与函数库就没有关系,移植方便
  • 缺点
    浪费空间和资源,所有相关的目标文件与牵涉的函数库被链接合成一个可执行文件,静态库对程序更新、部署和发布会带来麻烦,静态库更新,则所有使用它的应用程序都需要重新编译、发布给用户。

动态链接库(类似插件化,编译时不要求存在)

  • 优点
    可以实现进程之间资源共享(因此动态库也称为共享库),使程序升级变得简单。适用于大规模的软件开发,使开发过程独立、耦合度小,便于不同开发者和开发组织之间进行开发和测试
  • 缺点
    使用静态链接生成的可执行文件体积较大,包含相同的公共代码,造成浪费;使用动态链接库的应用程序不是自完备的,它依赖的so模块也要存在,如果使用载入时动态链接,程序启动时发现so不存在,系统将终止程序并给出错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值