当前位置: 首页 > 工具软件 > cpu_features > 使用案例 >

Android NDK添加NEON以及cpufeatures支持

罗俭
2023-12-01

本人使用Android studio3.0进行NDK开发,由于Android develop官网文档是针对2.2版本以下,这里为2.2以上版本的cmakelist配置做以下纪录:

一、添加NEON支持:

在build.gradle(app)中添加:

externalNativeBuild {
    cmake {
        cppFlags ""
        arguments "-DLOCAL_ARM_NEON=true" //NEON支持
    }
}

二、添加cpufeatures支持:

1.添加cpufeatures静态库:

include_directories(${ANDROID_NDK}/sources/android/cpufeatures)
add_library(cpufeatures STATIC
        ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)

2.配置参数:

if (${ANDROID_ABI} STREQUAL "armeabi-v7a")
        # make a list of neon files and add neon compiling flags to them
        set(neon_SRCS src/main/cpp/native-lib.cpp)

        set_property(SOURCE ${neon_SRCS}
                APPEND_STRING PROPERTY COMPILE_FLAGS " -mfpu=neon")
        add_definitions("-DHAVE_NEON=1")
        #add_definitions("-DLOCAL_ARM_NEON=1")
elseif (${ANDROID_ABI} STREQUAL "x86")
        set(neon_SRCS src/main/cpp/native-lib.cpp)
        set_property(SOURCE ${neon_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS
                " -mssse3  -Wno-unknown-attributes \
                   -Wno-deprecated-declarations \
                   -Wno-constant-conversion \
                   -Wno-static-in-inline")
        add_definitions(-DHAVE_NEON_X86=1 -DHAVE_NEON=1)
        #add_definitions("-DLOCAL_ARM_NEON=1")
else ()
        set(neon_SRCS)
endif ()

3.添加到link:

target_link_libraries( # Specifies the target library.
        native-lib
        cpufeatures

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


target_include_directories(native-lib PRIVATE
        ${ANDROID_NDK}/sources/android/cpufeatures)
 类似资料: