一、简介:
cmake 是一个跨平台、开源的构建系统。它是一个集软件构建、测试、打包于一身的软件。它使用与平台和编译器独立的配置文件来对软件编译过程进行控制。
二、它的常用命令:
1. 指定最小的cmake版本:
cmake_minimum_required(VERSION 3.4.1)
2.设置变量
set(PaddleLite_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../Lite")
注:CMAKE_CURRENT_SOURCE_DIR是当前CMakeLists.txt所在目录
3.添加库,类似addLibrary
include_directories(${PaddleLite_DIR}/cxx/include)
4. aux_source_directory 查找在某个路径下的所有源文件。
搜集所有在指定路径下的源文件的文件名,将输出结果列表储存在指定的变量中
aux_source_directory(. SOURCES)
5.add_library 将指定cpp文件生成.so动态库
add_library(
# Sets the name of the library.
Native
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
${SOURCES})
6.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)
7. set_target_properties导入库
set_target_properties(
# Specifies the target library.
paddle_light_api_shared
# Specifies the parameter you want to define.
PROPERTIES
IMPORTED_LOCATION
${PaddleLite_DIR}/cxx/libs/${ANDROID_ABI}/libpaddle_light_api_shared.so
# Provides the path to the library you want to import.
)
8. target_link_libraries
指定链接给定目标和/或其依赖项时要使用的库或标志。将传播链接库目标的使用要求。目标依赖项的使用要求会影响其自身源的编译。
(1)重命名
# SET_TARGET_PROPERTIES (hello_static PROPERTIES OUTPUT_NAME "hello")
将名为 hello_static 的静态库重命名为hello
(2)链接库
target_link_libraries(
# Specifies the target library.
Native
paddle_light_api_shared
)
指定 CMake 应链接到目标库的库
(3)清理同名的静态/动态库
cmake在构建一个新的target时,会尝试清理掉其他使用这个名字的库,比如hello.a跟hello.so
SET_TARGET_PROPERTIES (hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
(4)指定动态库及API版本
SET_TARGET_PROPERTIES (hello_static PROPERTIES VERSION 1.1 SOVERSION 1)
VERSION指代动态库版本,SOVERSION指代API版本。
9. add_custom_command
这个命令比较常见的是为了复制文件
add_custom_command(
TARGET Native
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy
${PaddleLite_DIR}/cxx/libs/${ANDROID_ABI}/libc++_shared.so
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libc++_shared.so)
POST_BUILD表示在执行target中的所有其它规则后运行
参数 | 含义 |
---|---|
PRE_BUILD | 在目标中执行任何其他规则之前运行 |
PRE_LINK | 在编译源代码之后,链接二进制文件或库文件之前运行 |
POST_BUILD | 在目标内所有其他规则均已执行后运行 |
TARGET:指定命令运行的目标;
COMMAND:指定要在构建时执行的命令行
由于篇幅跟能力有限,粗略讲了一下一些命令的常见使用,有机会再更新 命令详细使用