CMake:使用c文件链接共享库

时间:2017-08-17 21:09:18

标签: android c cmake libraries

我有一个包含多个liballnet.so文件的外部共享库.c。我想在CMake中将此库与astart.c相关联。

我使用My CMakeList.txt进行了以下操作:

cmake_minimum_required(VERSION 3.4.1)

add_library( astart

             SHARED

             src/main/myC/astart.c )

add_library( allnet

             # Sets the library as a shared library.
             SHARED

             IMPORTED )

set_target_properties(allnet PROPERTIES IMPORTED_LOCATION /Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/liballnet.so )

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       astart
                       allnet

                       ${log-lib} )

这是我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.example.hippolytelacassagne.allnet_2"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
            // Similar to other properties in the defaultConfig block,
            // you can configure the ndk block for each product flavor
            // in your build configuration.
            ndk {
                // Specifies the ABI configurations of your native
                // libraries Gradle should build and package with your APK.
                abiFilters 'armeabi-v7a'
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    // If you want Gradle to package prebuilt native libraries
    // with your APK, modify the default source set configuration
    // to include the directory of your prebuilt .so files as follows.
    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

我收到此错误:

    [1/3] Linking C shared library ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libastart.so
FAILED: : && /Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang  --target=armv7-none-linux-androideabi --gcc-toolchain=/Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=19 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security  -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a --sysroot /Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/platforms/android-19/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libastart.so -o ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libastart.so CMakeFiles/astart.dir/src/main/myC/astart.c.o  ../../../../src/main/myC/liballnet.so ../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libifaddrs.so -llog -lm && :
/Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: ../../../../src/main/myC/liballnet.so:1:1: invalid character
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:998: error: undefined reference to 'get_option'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:998: error: undefined reference to 'log_to_output'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1029: error: undefined reference to 'malloc_or_fail'
/Users/hippolytelacassagne/AndroidStudioProjects/A
llnet_2/app/src/main/myC/astart.c:1032: error: undefined reference to 'strcpy_malloc'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1042: error: undefined reference to 'malloc_or_fail'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1048: error: undefined reference to 'malloc_or_fail'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1099: error: undefined reference to 'init_log'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1101: error: undefined reference to 'log_print'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1104: error: undefined reference to 'log_print'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1109: error: undefined reference to 'log_print'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1178: error: undefined reference to 'adht_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1178: error: undefined reference to 'acache_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1178: error: undefined reference to 'traced_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1178: error: undefined reference to 'keyd_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:1178: error: undefined reference to 'keyd_generate'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:917: error: undefined reference to 'malloc_or_fail'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:279: error: undefined reference to 'log_print'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:704: error: undefined reference to 'abc_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:757: error: 
undefined reference to 'ad_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:605: error: undefined reference to 'alocal_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:661: error: undefined reference to 'aip_main'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:411: error: undefined reference to 'acache_save_data'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:369: error: undefined reference to 'strcat3_malloc'
/Users/hippolytelacassagne/AndroidStudioProjects/Allnet_2/app/src/main/myC/astart.c:579: error: undefined reference to 'allnet_htons'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
:app:externalNativeBuildDebug FAILED

似乎CMake没有链接2个文件,因为所有函数都被称为" undefined"在外部库liballnet.so中包含的文件中定义。

有人有想法吗?

我在Android Studio中使用CMake。

0 个答案:

没有答案