CMakeList编译报错ninja: error: missing and no known rule to make it解决方法

Android NDK开发引用第三方库CMakeList.txt编译报错:

ninja: error: '/XXX/NDKApplication/app/src/main/cpp/src/main/cpp/armeabi-v7a/libavcodec.so',

needed by '/XXX/NDKApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libhf_code.so', missing and no known rule to make it

原因是CMakeList.txt 里的配置有误,我用的版本是3.10.2

原因一:文件路径或文件名配置错误

cmake_minimum_required(VERSION 3.10.2)
add_library( # Sets the name of the library.
        hf_code
        SHARED
        native_test.cpp #这里路径或文件名配置有误
        )

原因二:target_link_libraries 链接第三方库没有使用${}包起来

add_library( swscale-5
        SHARED
        IMPORTED)
set_target_properties( swscale-5
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libswscale.so)


target_link_libraries(

     
        ${swscale-5} #链接第三方库需要${}

        ${log-lib} )

附一份导入了FFmpeg库的完整CMakeList.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.10.2)


#打印LOG
#message(STATUS "Cmake build type is: "${CMAKE_BUILD_TYPE})
#message(STATUS "Cmake build android abi is: "${ANDROID_ABI})

find_library(
        log-lib  # Android内置的log模块, 用于将JNI层的log打到AS控制台
        log )

#该指令的主要作用就是将指定的源文件生成链接文件,然后添加到工程中去
add_library( # Sets the name of the library.
        hf_code
        SHARED
        native_test.cpp
        )

#set_target_properties的最后一个参数,只要指定so库的路径让CMake在编译时可以找到就可以了
add_library( avcodec  # 库名字
        SHARED
        IMPORTED)
set_target_properties( avcodec
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavcodec.so)

add_library( avdevice
        SHARED
        IMPORTED)
set_target_properties( avdevice
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavdevice.so)

add_library( avfilter-7
        SHARED
        IMPORTED)
set_target_properties( avfilter-7
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavfilter.so)

add_library( avformat
        SHARED
        IMPORTED)
set_target_properties( avformat
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavformat.so)

add_library( avutil-56
        SHARED
        IMPORTED)
set_target_properties( avutil-56
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavutil.so)

add_library( swresample-3
        SHARED
        IMPORTED)
set_target_properties( swresample-3
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libswresample.so)

add_library( swscale-5
        SHARED
        IMPORTED)
set_target_properties( swscale-5
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libswscale.so)

include_directories(src/main/cpp/include)

target_link_libraries(

        hf_code

        ${avcodec}
        ${avdevice}
        ${avfilter-7}
        ${avformat}
        ${avutil-56}
        ${swresample-3}
        ${swscale-5}

        ${log-lib} )

build.gradle配置

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.ndk"
        minSdkVersion 29
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi-v7a'
        }
        externalNativeBuild {
            cmake {
                //cppFlags "-std=c++11 -frtti -fexceptions"
                // openssl 默认构建 armeabi-v7a,
                // arm64-v8a 64位要参考:
                // https://blog.csdn.net/github_35041937/article/details/102898087
                // https://blog.csdn.net/hnlylyb/article/details/80751220
                //arguments "-DANDROID_ABI=armeabi-v7a"

                cppFlags ""
                cFlags ""
                //指定需要编译的cpu架构
                abiFilters "armeabi-v7a"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
    ndkVersion '22.1.7171670'//as 4+ 新版在这里配ndk
    

}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}

项目结构

CMakeList编译报错ninja: error: missing and no known rule to make it解决方法_第1张图片

你可能感兴趣的:(cmake配置,ndk,cmake)