1.背景
有时候,我们有很多写得很好的C/C++库,但现在移动开发这么流行,如android,它使用Java。其实没有关系,Java可以通过JNI调用C/C++代码。
Java要调用C/C++,你往往要把它们编译成一个SO库,这个时候就需要用到CMakeLists。
2.举例说明
我作为一个程序猿,发现自己在学习一个知识点时,往往需要一个demo。好!上demo,并在demo中进行解析。
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
#设置CXX_FLAGS,保存调试信息、O0优化等级等
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O0")
#message可以用来在CMakelists打印
message(${ANDROID_ABI})
message(${PROJECT_SOURCE_DIR})
message(${PROJECT_SOURCE_DIR})
add_library(
# 设置你要编译库的名字.
adparser
adparser
# 设置你要编译的库为动态库.
SHARED
SHARED
# 列出要编译的原文件.
${PROJECT_SOURCE_DIR}/src/main/cpp/ADParser/udp_socket.cpp
${PROJECT_SOURCE_DIR}/src/main/cpp/ADParser/****.cpp )
${PROJECT_SOURCE_DIR}/src/main/cpp/ADParser/udp_socket.cpp
${PROJECT_SOURCE_DIR}/src/main/cpp/ADParser/****.cpp )
#设置你编译时,要引用的第三方其它库.
add_library( data_parser
SHARED
IMPORTED )
SHARED
IMPORTED )
set_target_properties(
data_parser
data_parser
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
#imported-lib/src/${ANDROID_ABI}/libimported-lib.so
${PROJECT_SOURCE_DIR}/src/imported-lib/jniLibs/${ANDROID_ABI}/libdata_parser.so)
#imported-lib/src/${ANDROID_ABI}/libimported-lib.so
${PROJECT_SOURCE_DIR}/src/imported-lib/jniLibs/${ANDROID_ABI}/libdata_parser.so)
#引用的头文件目录
include_directories( ${PROJECT_SOURCE_DIR}/src/imported-lib/include/
${PROJECT_SOURCE_DIR}/src/imported-lib/include/*** )
${PROJECT_SOURCE_DIR}/src/imported-lib/include/*** )
find_library( # Sets the name of the path variable.
log-lib
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# you want CMake to locate.
log )
target_link_libraries( #编译时,要链哪些库?
adparser
# Links the target library to the log library
# included in the NDK.
${log-lib}
data_parser
)
# included in the NDK.
${log-lib}
data_parser
)
本文介绍了在Android开发中,如何针对JNI模块编写和配置CMakeLists.txt文件,以便正确编译和链接C/C++代码,实现Android应用与本地库的交互。

1159

被折叠的 条评论
为什么被折叠?



