ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

AS项目添加C/C++代码

2021-10-26 13:59:36  阅读:225  来源: 互联网

标签:... CMake 代码 library 添加 C++ native your build


项目添加 C 和 C++ 代码

创建新的Native工程

勾选支持c++即可,生成的项目结构如下:
在这里插入图片描述

现有工程中添加Native代码

  • 创建cpp目录:在src/main目录下创建cpp目录;
  • 创建cpp文件:New > C/C++ Source File,想要创建头文件需勾选 Create an associated header 复选框;
  • 创建CMakeLists.txt:在module根目录下创建txt文件;
  • 关联gradle:见【使用GUI关联gradle】和【手动关联gradle】两个章节;
  • 应用更改和更新:首次关联CMakeLists.txt或者Android.mk之后,需点击Sync Project 图标应用修改;后面修改相关脚本后应该从菜单栏中依次选择 Build > Refresh Linked C++ Projects将更改进行同步。

使用GUI关联gradle

在module根目录右键,选择 Link C++ Project with Gradle,可以选择CMake 或 ndk-build。如果选择CMake则需指定CMakeLists.txt 脚本文件,如果选择 ndk-build指定Android.mk 脚本文件(如果 Application.mk 文件与您的 Android.mk 文件位于同一目录下,Android Studio 也会包含此文件)

在这里插入图片描述
在这里插入图片描述

手动关联gradle

只需将 externalNativeBuild 块添加到模块级 build.gradle 文件中,并使用 cmakendkBuild 块对其进行配置:

android {
  ...
  defaultConfig {...}
  buildTypes {...}

  // Encapsulates your external native build configurations.
  externalNativeBuild {

    // Encapsulates your CMake build configurations.
    cmake {

      // Provides a relative path to your CMake build script.
      path "CMakeLists.txt"
    }
    // or ndkBuild {...}
  }
}

还支持利用变体为每个变体指定可选配置:

android {
  ...
  defaultConfig {
    ...
    // This block is different from the one you use to link Gradle
    // to your CMake or ndk-build script.
    externalNativeBuild {

      // For ndk-build, instead use the ndkBuild block.
      cmake {

        // Passes optional arguments to CMake.
        arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"

        // Sets a flag to enable format macro constants for the C compiler.
        cFlags "-D__STDC_FORMAT_MACROS"

        // Sets optional flags for the C++ compiler.
        cppFlags "-fexceptions", "-frtti"
      }
    }
  }

  buildTypes {...}

  productFlavors {
    ...
    demo {
      ...
      externalNativeBuild {
        cmake {
          ...
          // Specifies which native libraries or executables to build and package
          // for this product flavor. The following tells Gradle to build only the
          // "native-lib-demo" and "my-executible-demo" outputs from the linked
          // CMake project. If you don't configure this property, Gradle builds all
          // executables and shared object libraries that you define in your CMake
          // (or ndk-build) project. However, by default, Gradle packages only the
          // shared libraries in your app.
          targets "native-lib-demo",
                  // You need to specify this executable and its sources in your CMakeLists.txt
                  // using the add_executable() command. However, building executables from your
                  // native sources is optional, and building native libraries to package into
                  // your app satisfies most project requirements.
                  "my-executible-demo"
        }
      }
    }

    paid {
      ...
      externalNativeBuild {
        cmake {
          ...
          targets "native-lib-paid",
                  "my-executible-paid"
        }
      }
    }
  }

  // Use this block to link Gradle to your CMake or ndk-build script.
  externalNativeBuild {
    cmake {...}
    // or ndkBuild {...}
  }
}

CMakeLists.txt示例文件


# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add_library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

# Specifies a path to native header files.
include_directories(src/main/cpp/include/)

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       native-lib

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

调试原生代码

使用 LLDB 来调试代码

参考资料

标签:...,CMake,代码,library,添加,C++,native,your,build
来源: https://blog.csdn.net/csfchh/article/details/118740046

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有