在另一个 ndkbuild 库上制作 CMake 本机库

Making CMake native library on another ndkbuild library

我有两个 Android 库需要为我的应用程序构建。依赖图如下:

Myapp --> ProjectA --> ProjectF

Myapp 依赖于 ProjectA(这是一个库项目),ProjectA 依赖于 ProjectF(也是一个库项目)。

ProjectAProjectF 都包含本机代码。

ProjectA 是使用 CMake(我写的)构建的,ProjectF 是我从 Github 下载的项目,并使用 ndkbuild(包含 Android.mk)。它从命令行构建良好。我为 ProjectF 创建了一个 build.gradle,以便将其包含在应用程序的 Android Studio 项目中。 build.gradle 如下所示。

apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }

    externalNativeBuild {
        ndkBuild {
            path 'jni/Android.mk'
        }
    }
}

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

我已经在上面的项目结构中设置了依赖。

我的 CMakeLists.txt ProjectA 文件如下所示:

cmake_minimum_required(VERSION 3.4.1)

add_library(
             my-native-lib
             SHARED
             [List of .c/.cpp source files for ProjectA]
            )

include_directories(
        [list of include directories for ProjectA and API headers from ProjectF]
        )

add_library(
        ProjectF   #[this is the library which should be linked when building ProjectA]

        STATIC
        IMPORTED
        )

find_library(
        log-lib

        log )


target_link_libraries(
        my-native-lib

        ${log-lib} )

但是,在构建 ProjectA 时,对于从 ProjectF 引用的符号,我遇到了链接器错误。链接器命令显示来自 ProjectF 的库根本没有链接。

我确信我遗漏了一些非常愚蠢的东西。如果有任何帮助,我将不胜感激。

未链接 ProjectF 目标库,因为它未包含在您的 target_link_libraries() call. CMake will not link this library to other libraries automatically; you have to explicitly tell CMake which libraries to link. You likely need to tell CMake where your imported ProjectF library resides on your machine as well, using the IMPORTED_LOCATION 属性 中。

此外,find_library() 通常与 PATHS 参数一起调用,以告诉 CMake 在何处查找特定库。如果找不到您的 log 库,请考虑添加一些搜索路径。

您的 CMake 代码的更新部分可能如下所示:

add_library(
        ProjectF   #[this is the library which should be linked when building ProjectA]
        STATIC
        IMPORTED
        )

# Tell CMake where ProjectF library exists.
set_target_properties(ProjectF PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/path/to/libs/ProjectFLib.a
        )

find_library(
        log-lib
        PATHS /path/containing/your/log/lib
        log )

# Don't forget to link ProjectF here!
target_link_libraries(
        my-native-lib PUBLIC
        ProjectF
        ${log-lib} )