Java Android studio中使用Cmake函数的未定义引用

Java Android studio中使用Cmake函数的未定义引用,java,c,android-studio,cmake,undefined-reference,Java,C,Android Studio,Cmake,Undefined Reference,我尝试将C函数添加到我的Java Android应用程序中。 我已经在C中运行了一个基本函数Hello World。 在这个文件hello.c中,我调用另一个文件(astart.c)中包含的另一个c函数,该文件本身调用另一个文件(util.c)中的另一个函数c,该文件调用其他文件中的其他函数。 没问题,不是吗 我将库添加到我的CMakeLists.txt中: # Sets the minimum version of CMake required to build the native # li

我尝试将C函数添加到我的Java Android应用程序中。
我已经在C中运行了一个基本函数Hello World。
在这个文件hello.c中,我调用另一个文件(astart.c)中包含的另一个c函数,该文件本身调用另一个文件(util.c)中的另一个函数c,该文件调用其他文件中的其他函数。
没问题,不是吗

我将库添加到我的CMakeLists.txt中:

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
         native-lib

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         # Associated headers in the same location as their source
         # file are automatically included.
         src/main/cpp/native-lib.cpp )

add_library( hello

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/hello.c )

add_library( astart

         # Sets the library as a shared library.
        SHARED

         src/main/myCpp/astart.c )

add_library( ai

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/ai.c )

add_library( sha

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/sha.c )

add_library( util

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/util.c )

add_library( allnet_log

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/allnet_log.c )

#add_library( packet

         # Sets the library as a shared library.
#            SHARED

#           src/main/myCpp/lib/packet.c )

add_library( configfiles

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/configfiles.c )

add_library( allnet_queue

         # Sets the library as a shared library.
         SHARED

         src/main/myCpp/lib/allnet_queue.c )



# Add the directories where the Cpp header files are to let CMake             find them during compile time
include_directories(src/main/myCpp/)
include_directories(src/main/myCpp/lib/)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                   native-lib

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )
我的gradle文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
    applicationId "com.example.hippolytelacassagne.allnet"
    minSdkVersion 19
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner    "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            cppFlags ""
            cFlags "-fexceptions"
        }
    }
}
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 'imported-lib/src/', 'more-imported-   libs/src/'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
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'
testCompile 'junit:junit:4.12'
}
我的hello.c在Home.java中调用:

#include <jni.h>

#include "lib/app_util.h"
#include "lib/packet.h"
#include "astart.c"

JNIEXPORT
jstring
JNICALL
Java_com_example_hippolytelacassagne_allnet_Home_Hello(
JNIEnv *env,
jobject callingObject)
{
    char * args [] = { "allnet", "-v", "def", NULL };
    astart_main(3, args);
    return (*env)->NewStringUTF(env, "Hello World");
}
错误似乎来自util.c.的某些函数。
我不明白,因为假定没有定义的函数是在包含的文件中定义的

有人有主意吗?谢谢。

我发现了问题。
Android Studio需要指定CmakeList中文件之间的依赖关系:

target_link_libraries( # Specifies the target library.
                   util
                   sha
                   ai
                   allnet_log

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )
文件中包含的内容不够

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <netdb.h>  /* h_errno */
#include <dirent.h>  /* h_errno */
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "packet.h"
#include "mgmt.h"
#include "allnet_log.h"
#include "util.h"
#include "ai.h"
#include "sha.h"
    FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing '/Users/hippolytelacassagne/Library/Android/sdk/cmake/3.6.4111459/bin/cmake' with arguments {--build /Users/hippolytelacassagne/AndroidStudioProjects/Allnet/app/.externalNativeBuild/cmake/debug/mips64 --target util}
  [1/2] Building C object CMakeFiles/util.dir/src/main/myCpp/lib/util.c.o
  [2/2] Linking C shared library ../../../../build/intermediates/cmake/debug/obj/mips64/libutil.so
  FAILED: : && /Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang  --target=mips64el-none-linux-android --gcc-toolchain=/Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-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/mips64el-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fintegrated-as -Wa,--noexecstack -Wformat -Werror=format-security -fexceptions -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a --sysroot /Users/hippolytelacassagne/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libutil.so -o ../../../../build/intermediates/cmake/debug/obj/mips64/libutil.so CMakeFiles/util.dir/src/main/myCpp/lib/util.c.o  -lm && :
  CMakeFiles/util.dir/src/main/myCpp/lib/util.c.o: In function `mgmt_to_string':
  /Users/hippolytelacassagne/AndroidStudioProjects/Allnet/app/src/main/myCpp/lib/util.c:245: undefined reference to `ia_to_string'
  /Users/hippolytelacassagne/AndroidStudioProjects/Allnet/app/src/main/myCpp/lib/util.c:274: undefined reference to `ia_to_string'
  CMakeFiles/util.dir/src/main/myCpp/lib/util.c.o: In function `init_packet':
  /Users/hippolytelacassagne/AndroidStudioProjects/Allnet/app/src/main/myCpp/lib/util.c:532: undefined reference to `sha512_bytes'
  CMakeFiles/util.dir/src/main/myCpp/lib/util.c.o: In function `print_gethostbyname_error':
  /Users/hippolytelacassagne/AndroidStudioProjects/Allnet/app/src/main/myCpp/lib/util.c:1554: undefined reference to `log_print'
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED
target_link_libraries( # Specifies the target library.
                   util
                   sha
                   ai
                   allnet_log

                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )