Android已有工程支持c++ 使用CMake

本文介绍如何在Android项目中添加C++支持。主要包括:通过CMakeLists.txt引入C++源文件,配置build.gradle以编译指定平台的代码,并链接外部库文件。文中详细展示了CMakeLists.txt文件的编写规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 已有项目添加c++支持
1、右键module新建CMakeLists.txt
2.module中build.gradle配置文件中在

  android{
     defaultConfig {
         externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        ndk {//编译在哪个CPU平台上
            abiFilters 'armeabi-v7a'
        }
      sourceSets.main {//外部so文件路径
            jniLibs.srcDir 'libs'
        }
    }
    externalNativeBuild {

        // Encapsulates your CMake build configurations.
        cmake {

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



3、CMakeLists.txt文件编写规则

cmake_minimum_required(VERSION 3.4.1)

add_library( # 为library起名字
             native-lib
             # 设置为SHARE类型,STATIC为需要加载.a文件
             SHARED
             # 需要编译的源代码文件目录
             src/main/cpp/native-lib.cpp )
add_library( avcodec-57# 为library起名字
             SHARED
             IMPORTED ) # 不需要编译,由外部导入
set_target_properties( avcodec-57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavcodec-57.so )//配置导入文件的路径
add_library( avfilter-6
             SHARED
             IMPORTED )
set_target_properties( avfilter-6
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavfilter-6.so )
add_library( avformat-57
             SHARED
             IMPORTED )
set_target_properties( avformat-57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavformat-57.so )
add_library( avutil-55
             SHARED
             IMPORTED )
set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavutil-55.so )
add_library( swresample-2
             SHARED
             IMPORTED )
set_target_properties( swresample-2
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswresample-2.so )
add_library( swscale-4
             SHARED
             IMPORTED )
set_target_properties( swscale-4
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswscale-4.so )
include_directories( libs/include )#外部库需要的头文件
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 )//加载NDK上的library
# 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.
#将需要的库文件进行链接
target_link_libraries( # Specifies the target library.
                       native-lib
                       avcodec-57
                       avfilter-6
                       avformat-57
                       avutil-55
                       swresample-2
                       swscale-4
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )



转载于:https://www.cnblogs.com/qcjd/p/9324903.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值