简书链接:cmake的几个常用语法整理
文章字数:495,阅读全文大约需要1分钟
1、find_library
寻找库
此命令用于查找库。创建名为的缓存条目以存储此命令的结果。如果找到库,则结果存储在变量中,除非清除变量,否则不会重复搜索。如果找不到任何内容,结果将是 -NOTFOUND,并且下次使用相同变量调用find_library时将再次尝试搜索。

1
2
3
4
5
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 存储为log-lib变量
https://cmake.org/cmake/help/v3.6/command/find_library.html
其它find_也类似

2、target_link_libraries
链接库 也就是把当前库和其他库合并

链接静态库 通常做导入操作,很少用到
1
2
3
4
5
6
7
8
9
10
11
12
13
14

3、```add_library```
添加库 同时是把源码合并生成一个动态库名
通常执行add_library之后调用```target_link_libraries```
4、```include_directories``` 添加头文件目录
5、```add_subdirectory```添加cmake目录
6、```set_target_properties```
可以把一个静态库变成一个变量然后通过
7、```target_link_libraries```合并

8、```find_path```
9、```find_package```
10、```aux_source_directory(directory varname)``` 添加多个c++’文件 参数1为目录名,2为变量名, 就是把多个东西融合为一个变量,也可以用```.```融合当前所有。
### 使用本地文件静态库举例

#[[
include_directories(otheheadfile) #这个源文件和另外一个文件的根目录不是同一个,也可以达到识别效果。

file(GLOB SRCS “${PROJECT_SOURCE_DIR}/jni/global/*.cpp”)
add_executable(callstatic callstatic.c)
add_library(mystatic
STATIC
IMPORTED)
set_target_properties(mystatic
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/lib/libmystatic.a)
target_link_libraries(callstatic mystatic)
message(“compile call staticlib…. c use c static lib”)
message(“————————————————“)

]]

1
2
3

### 使用cmake产生的静态库举例
假设cmake中有一句话

add_library(qssqstatic STATIC xxxx)

1
那么执行的话是

set_target_properties(qssqstatic PROPERTIES OUTPUT_NAME “qssqstatic”)
target_link_libraries(wxplus2 qssqstatic )

1
2
3

这里```wxplus2```就是动态库或者可执行文件,而```qssqstatic```是```add_library```产生的静态库
### 使用动态库举例

#[[
include_directories(otheheadfile)

file(GLOB SRCS “${PROJECT_SOURCE_DIR}/jni/global/*.cpp”)
add_executable(ccallcdynamic calldynamic.c)

static need set_target_properties() but dynamic not need

link_directories(lib)#从 lib目录查找

设置第三方动态库属性(存储位置) armeabi-v7a

add_library(MYDYNAMIC
SHARED
IMPORTED)

set_target_properties(MYDYNAMIC PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/lib/libmydynamic.so)
target_link_libraries(ccallcdynamic MYDYNAMIC)

message(“compile ccallcdynamic ….”)
message(“————————————————“)
]]



更多举例参考我github源码


这个文章也写的不错
https://www.jianshu.com/p/8909efe13308