CmakeList使用流程
(一)目前未安装Cmake,所以基于android studio 平台编译
(1)在android工程项目下的src/main中创建jni文件夹,jin文件夹中存放相印的头文件和cpp文件
(2)在android 工程项目下创建CMakeLists.txt 文本
(3)在CMakeLists.txt 中输入的信息如下
同样CMakeLists使用#字符进行注释
cmake_minimum_required(VERSION 3.4.1) #设置cmake编译请求版本,一般固定为此
#设置Cmake操作的相关信息
message(STATUS "******************************************************************")
message(STATUS "CMAKE_SOURCE_DIR->" ${CMAKE_SOURCE_DIR})
message(STATUS "PROJECT_SOURCE_DIR->)" ${PROJECT_SOURCE_DIR})
message(STATUS "******************************************************************")
#${CMAKE_SOURCE_DIR} 为当前CMakeLists所在的路径
#1.搜索源文件并赋值给变量名
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/jni CLIENT_SRC) #在android工程的main中创建jni文件为该工程编译CMaskeLists的路径
#2.设置源文件到统一的变量
set(DIR_SRCS ${CLIENT_SRC} )
#3.设置第三方库头文件所在位置
include_directories(${CMAKE_SOURCE_DIR}/src/main/jni/include/
${CMAKE_SOURCE_DIR}/src/main/jni/
${CMAKE_SOURCE_DIR}/src/main/jni/include/libxml/
${CMAKE_SOURCE_DIR}/src/main/jni/include/openssl/
)
#4.设置第三方库头库所在位置
#在android工程main下创建jniLibs文件,存放多种版本第三方提供的库版本
#${ANDROID_ABI}为编译所需的第三方库,设置则编译器会自动识别所需版本
link_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/)
#5.对应的库
link_libraries(PDI_CPP.so) #加载第三方的动态库,名称即可不需要lib
link_libraries(encrypt_m.so)
link_libraries(iconv.a) #加载第三方的静态库,
#6.创建动态库
add_library( # Sets the name of the library.
Sellib
# Sets the library as a shared library. #SHARED 指定为动态库
SHARED
# Provides a relative path to your source file(s).
${DIR_SRCS}
${CMAKE_SOURCE_DIR}/src/main/jni/ghasd.cpp #指定加载的c/c++文件
)
#指定所需的头文件路径
include_directories( ${CMAKE_SOURCE_DIR}/src/main/jni/include/
${CMAKE_SOURCE_DIR}/src/main/jni/
${CMAKE_SOURCE_DIR}/src/main/jni/include/libxml/
${CMAKE_SOURCE_DIR}/src/main/jni/include/openssl/
)
#设置CMakeLists支持c++11标准
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# 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.
#关联系统自带的log库
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( # Sets the name of the path variable.
jnigraphics-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
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.
#7.链接需要的第三方动态库
target_link_libraries( # Specifies the target library.
Sellib #目前想要生成的库名
PDI_CPP #第三方引入的库名
encrypt_m
iconv
log
# Links the target library to the log library
# included in the NDK.
${log-lib}
${jnigraphics-lib} )