美文网首页
Android NDK 开发之 CMake 编译 libpng

Android NDK 开发之 CMake 编译 libpng

作者: Mixqum | 来源:发表于2019-05-11 23:15 被阅读0次
    1. libpng是什么?
      专门对png进行读写,并且要与zlib同时使用

    2. Android 使用cmake编译libpng项目地址:
      https://github.com/kingdawin/libpng-android
      这个项目只需要libpng代码,而不用下载zlib,只需在cmake中关联Android系统的zlib库。由于Android Bitmap解压也用到libpng库,所以系统自身带有zlib。

    3. cmake配置相关知识

    • 库与库如何关联
      使用target_link_libraries(),在里面加入你需要关联的库即可
    # png库与zlib关联
    target_link_libraries( ${project_name} PUBLIC ZLIB::ZLIB )
    
    • 如何添加多个库
      使用add_library
    add_library( 
    #库名
    ${project_name} 
    #代码路径
    ${sources} 
    ${headers} )
    

    项目CMakeList.txt文件说明

    cmake_minimum_required( VERSION 3.6 )
    # png库名
    set( project_name png )
    project( ${project_name} )
    
    if( MSVC )
        add_definitions(/MP)
    endif()
    #jni代码路径,根据你实际路径修改
    set( source_dir ${CMAKE_CURRENT_SOURCE_DIR}/jni )
    #头文件路径
    set( headers
        ${source_dir}/pnginfo.h
        ${source_dir}/config.h
        ${source_dir}/png.h
        ${source_dir}/pngconf.h
        ${source_dir}/pngdebug.h
        ${source_dir}/pnglibconf.h
        ${source_dir}/pngprefix.h
        ${source_dir}/pngpriv.h
        ${source_dir}/pngstruct.h
    )
    # c文件路径
    set( sources
        ${source_dir}/arm/arm_init.c
        ${source_dir}/arm/filter_neon.S
        ${source_dir}/arm/filter_neon_intrinsics.c
        ${source_dir}/png.c
        ${source_dir}/pngerror.c
        ${source_dir}/pngget.c
        ${source_dir}/pngmem.c
        ${source_dir}/pngpread.c
        ${source_dir}/pngread.c
        ${source_dir}/pngrio.c
        ${source_dir}/pngrtran.c
        ${source_dir}/pngrutil.c
        ${source_dir}/pngset.c
        ${source_dir}/pngtest.c
        ${source_dir}/pngtrans.c
        ${source_dir}/pngwio.c
        ${source_dir}/pngwrite.c
        ${source_dir}/pngwtran.c
        ${source_dir}/pngwutil.c
    )
    # 链接系统的zlib库
    find_package( zlib REQUIRED )
    
    # If you want a shared library instead of static, specify the BUILD_SHARED_LIBS
    # variable (which is a built-in CMake option).
    # 添加libpng库
    add_library( ${project_name} ${sources} ${headers} )
    # png库与zlib关联
    target_link_libraries( ${project_name} PUBLIC ZLIB::ZLIB )
    target_include_directories( ${project_name} PUBLIC
        $<BUILD_INTERFACE:${source_dir}>
        $<INSTALL_INTERFACE:include>
    )
    #使用add_library可以添加多个库,比如自己定义一个native-jni
    #native-jni要使用png lib,只要将png lib关联即可在native-jni代码文件用
    # #include<png.h>引用相关函数
    add_library( 
    #你的库名
    native-jni 
    #填你的c/c++代码路径
    ${sources} 
    ${headers} )
    
    #下面的配置用来安装,可以删除
    ############################################################
    ## INSTALL
    ############################################################
    
    include( CMakePackageConfigHelpers )
    
    set( PNG_VERSION 1.6.29 )
    set( PNG_EXPORT_DIR share/png-${PNG_VERSION}/cmake )
    set( PNG_EXPORT_NAME png-export )
    
    install( TARGETS png EXPORT ${PNG_EXPORT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        INCLUDES DESTINATION include
    )
    
    install( FILES ${headers} DESTINATION include )
    install( EXPORT ${PNG_EXPORT_NAME} DESTINATION ${PNG_EXPORT_DIR} )
    
    configure_file( png-config.cmake.in png-config.cmake @ONLY )
    
    write_basic_package_version_file(
        png-config-version.cmake
        VERSION ${PNG_VERSION}
        COMPATIBILITY AnyNewerVersion
    )
    
    install(
        FILES
            ${CMAKE_CURRENT_BINARY_DIR}/png-config.cmake
            ${CMAKE_CURRENT_BINARY_DIR}/png-config-version.cmake
        DESTINATION ${PNG_EXPORT_DIR}
    )
    

    4.NDK环境配置
    1)下载LLDB,NDK,CMake
    as中打开sdk下载,选择SDK Tools,勾选LLDB任意版本和NDK

    image.png

    2)设置as的NDK路径

    5.在已有的项目添加Cmake文件,并与项目关联

    相关文章

      网友评论

          本文标题:Android NDK 开发之 CMake 编译 libpng

          本文链接:https://www.haomeiwen.com/subject/dwjxmqtx.html