Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android NDK JNI构建错误:未定义对';函数名';_Android_C_Cmake_Android Ndk_Java Native Interface - Fatal编程技术网

Android NDK JNI构建错误:未定义对';函数名';

Android NDK JNI构建错误:未定义对';函数名';,android,c,cmake,android-ndk,java-native-interface,Android,C,Cmake,Android Ndk,Java Native Interface,我有一个简单的Android NDK代码和JNI。不幸的是,由于以下错误,它无法生成: error: undefined reference to 'get_hello()' 我已经检查了其他带有相同错误的Stackoverflow问题。但它们都与我的文件结构不相似 文件结构 CMakeLists.txt native-lib.cpp 你好 你好/你好 你好/你好 Cmake误差 您的本机库在链接过程中失败,因为您没有告诉它在哪里可以找到get\u hello()的定义。您需要将包含get_

我有一个简单的Android NDK代码和JNI。不幸的是,由于以下错误,它无法生成:

error: undefined reference to 'get_hello()' 
我已经检查了其他带有相同错误的Stackoverflow问题。但它们都与我的文件结构不相似

文件结构 CMakeLists.txt native-lib.cpp 你好 你好/你好 你好/你好 Cmake误差
您的
本机库在链接过程中失败,因为您没有告诉它在哪里可以找到
get\u hello()
的定义。您需要将包含
get_hello()
定义的库链接到
本机库

您的代码有以下行:

target_link_libraries( hihi-lib
                       hello-lib
                       native-lib
                       ${log-lib} )
它将所有其他库链接到
hihi-lib
库。这可能不是你想要做的

查看代码,
native lib
依赖于
hello lib
hello lib
依赖于
hihi lib
。因此,您需要使用
target\u link\u libraries()
命令指定这些依赖项:

# Link hihi-lib to hello-lib.
target_link_libraries( hello-lib PUBLIC hihi-lib )

# Link hello-lib (and others) to native-lib, hihi-lib will be propagated via hello-lib.
target_link_libraries( native-lib PRIVATE hello-lib ${log-lib} )

注意,您应该始终使用作用域操作符(例如,
PUBLIC
PRIVATE
等)来指定使用时CMake链接库的方式。

谢谢您的回答!然而,我仍然得到同样的错误。我把你的<代码> TajixLink库行以相同的顺序提供(即,<代码> hello LIB < /C>第一,然后<代码>原生LIB < /C> >)。因为你正在混合C和C++代码,你仍然可能因为文档中的问题而接收到未定义的引用错误。为了解决这个问题,例如,您可以包装
const char*get_hello()声明在
extern“C”{…}
块中。不要将代码(原型除外)放在头文件中。不要将数据实例放在头文件中结构、原型、extern语句的定义放在头文件中
#include <jni.h>

#include "my_hello/hello.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {

    return env->NewStringUTF(get_hello());
}
#ifndef MY_APPLICATION_HELLO_H
#define MY_APPLICATION_HELLO_H

const char *get_hello();

#endif //MY_APPLICATION_HELLO_H
#include "hello.h"
#include "../your_hello/hihi.h"

const char *get_hello() {
    return get_your_hello();
}
#ifndef MY_APPLICATION_HIHI_H
#define MY_APPLICATION_HIHI_H

const char* get_your_hello();

#endif //MY_APPLICATION_HIHI_H
#include "hihi.h"

const char* get_your_hello() {
    return "your hello";
}
> Task :app:externalNativeBuildDebug FAILED
Build native-lib_armeabi-v7a
ninja: Entering directory `/home/myname/AndroidStudioProjects/MyApplication/app/.cxx/cmake/debug/armeabi-v7a'
[1/1] Linking CXX shared library /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
FAILED: /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so 
: && /home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi23 --gcc-toolchain=/home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/myname/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o /home/myname/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o  -latomic -lm && :
/home/myname/AndroidStudioProjects/MyApplication/app/src/main/cpp/native-lib.cpp:10: error: undefined reference to 'get_hello()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
target_link_libraries( hihi-lib
                       hello-lib
                       native-lib
                       ${log-lib} )
# Link hihi-lib to hello-lib.
target_link_libraries( hello-lib PUBLIC hihi-lib )

# Link hello-lib (and others) to native-lib, hihi-lib will be propagated via hello-lib.
target_link_libraries( native-lib PRIVATE hello-lib ${log-lib} )