java - 在 Android Studio 中使用 Cmake 未定义函数引用

标签 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
# 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");
}

如果在 hello.c 中调用了 astart.c,则包含:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <pwd.h>
#include <ifaddrs.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>      /* IFF_LOOPBACK, etc */
#include <arpa/inet.h>   /* inet_addr */

#include "lib/util.h"
#include "lib/allnet_log.h"
#include "lib/packet.h"
#include "lib/configfiles.h"
#include "lib/allnet_queue.h"

以及在 astart.c 中调用的 util.c 的包含:

#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"

这是 gradle 控制台的错误消息:

    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

错误似乎来自于 util.c 的某些函数。
我不明白,因为应该没有定义的功能在包含的文件中定义。

有人有想法吗?谢谢。

最佳答案

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

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} )

文件中的#includes 不够。

关于java - 在 Android Studio 中使用 Cmake 未定义函数引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45316915/

相关文章:

java - 请求元素不以 'Request'结尾时spring-ws生成的wsdl无效

android - com.android.dex.DexException : Multiple dex files define

android-studio - Gradle 项目同步失败并出现 ParseError

android - 在应用程序中导入我的 android 库不被识别为库

java - Android Activity会关闭Looper&Handler吗?

java - 是否有可能一次获得所有的 apache 公共(public)资源?

java - 无法从 Java 程序执行 R 脚本?

C 程序显示最多输入的 %2==0 个数字,直到您输入数字 %2!=0 && %7==0

c - 在 C 中使用两个 fork() 时如何利用每个进程

c - 程序的编译在执行前是否会在缓存中存储一​​些数据?? (C, Linux)