前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >L017 android cmake的使用

L017 android cmake的使用

作者头像
上善若水.夏
发布2018-09-28 11:08:48
9750
发布2018-09-28 11:08:48
举报
文章被收录于专栏:上善若水上善若水

android中c++标准的选择

C++ Standard

指定编译库的环境,其中Toolchain Default使用的是默认的CMake环境;C++ 11也就是C++环境。两种环境都可以编库,至于区别,后续会跟进,当前博文使用的是CMake环境

Exceptions Support

如果选中复选框,则表示当前项目支持C++异常处理,如果支持,在项目Module级别的build.gradle文件中会增加一个标识 -fexceptionscppFlags属性中,并且在so库构建时,gradle会把该属性值传递给CMake进行构建。

Runtime Type Information Support

同理,选中复选框,项目支持RTTI,属性cppFlags增加标识-frtti

CMakeLists.txt的配置

CMakeLists.txt 用于配置jni项目属性,主要用于声明CMake版本 so库名称 C/Cpp文件路径等信息。

注释

和bash中一样,使用“#”作为一行的注释。

cmake版本声明

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.4.1)

添加编译目标add_library()

配置库信息,库的名字,动态库或静态库,依赖的源文件

代码语言:javascript
复制
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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp
             src/main/cpp/test.cpp)

STATIC:静态库,是目标文件的归档文件,在链接其它目标的时候使用。 SHARED:动态库,会被动态链接,在运行时被加载。 MODULE:模块库,是不会被链接到其它目标中的插件,但是可能会在运行时使用dlopen-系列的函数动态链接。

配置头文件路径include_directories()

代码语言:javascript
复制
 include_directories("src/main/cpp")

查找链接库find_library

在指定目录下搜索一个库, 保存在变量log-lib中,如果没有指定路径,则使用默认系统路径

代码语言:javascript
复制
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 )

添加链接库 target_link_libraries

添加链接库,相同于指定-l参数

代码语言:javascript
复制
target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

参考链接

  1. 解决Android Studio 2.2.3中添加.cpp .h文件在Project->Android无法显示,无法正常编译问题
  2. cmake doc
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.01.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • android中c++标准的选择
    • C++ Standard
      • Exceptions Support
        • Runtime Type Information Support
        • CMakeLists.txt的配置
          • 注释
            • cmake版本声明
              • 添加编译目标add_library()
                • 配置头文件路径include_directories()
                  • 查找链接库find_library
                    • 添加链接库 target_link_libraries
                    • 参考链接
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档