链接

时间:2016-10-01 10:35:32

标签: gradle android-ndk cmake

我正在构建一个非平凡的android库,使用gradle android build tools v2.2.0,链接到十几个预构建的.a文件,输出应该是.so文件。在尝试从ndk-build转换为cmake时,我无法正确链接.so文件,因为生成的ninja版本似乎无法找到静态库'头。

的CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_VERBOSE_MAKEFILE on)

include_directories(${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/prebuilt/include )

add_library(precompiledA STATIC IMPORTED)
set_target_properties(precompiledA PROPERTIES 
    IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/dcmtk/lib/libprecompiledA.a)

add_library(precompiledB STATIC IMPORTED)
set_target_properties(precompiledB PROPERTIES 
    IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/dcmtk/lib/libprecompiledB.a)

add_library(precompiledC STATIC IMPORTED)
set_target_properties(precompiledC PROPERTIES 
    IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/dcmtk/lib/libprecompiledC.a)



add_library( jni-library

             SHARED

             hello.cpp )

find_library( log-lib
              log )

target_link_libraries( jni-library 

                       precompiledA
                       precompiledB
                       precompiledC

                       ${log-lib} )

的build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 24

        ndk {
            abiFilters 'armeabi-v7a'
        }

        externalNativeBuild {
            cmake {
                arguments '-DANDROID_STL=gnustl_static',
                          '-DANDROID_CPP_FEATURES=exceptions'
            }
        }
    }

    externalNativeBuild {
        cmake {
            path 'src/main/cpp/CMakeLists.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

错误 - 缩写

ld: error: cannot find -lprecompiledA
undefined references galore...

当涉及到调用gradle时,会生成.o文件,但在链接时,与静态库关联的标头似乎丢失,链接失败。是否可以选择告诉cmake静态库标题的链接时间在哪里?

1 个答案:

答案 0 :(得分:1)

更正未定义的引用

我犯的错误是没有使用target_include_directories,没有给出库的绝对路径,忘记了${ANDROID_ABI}变量来获取正确版本的二进制文件。

cmake_minimum_required( VERSION 3.4.1 )

set( CMAKE_VERBOSE_MAKEFILE on )

add_library( precompiledA STATIC IMPORTED )
set_target_properties( precompiledA 
                       PROPERTIES 
                       IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/prebuilt_libs/mylib/lib/${ANDROID_ABI}/libprecompiledA.a )

add_library( precompiledB STATIC IMPORTED )
set_target_properties( precompiledB 
                       PROPERTIES 
                       IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/prebuilt_libs/mylib/lib/${ANDROID_ABI}/libprecompiledB.a )

add_library( precompiledC STATIC IMPORTED )
set_target_properties( precompiledC 
                       PROPERTIES 
                       IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/prebuilt_libs/mylib/lib/${ANDROID_ABI}/libprecompiledC.a )

find_library( log-lib
              log )


target_include_directories( dicom-jni PRIVATE
                            prebuilt_libs/mylib/${ANDROID_ABI}/include )

add_library( jni-library
             SHARED
             hello.cpp )

target_link_libraries( jni-library 

                       precompiledA
                       precompiledB
                       precompiledC

                       ${log-lib} )