Android ndk-build链接器无法找到预构建的库函数

时间:2016-09-14 09:38:29

标签: android android-ndk ndk-build

我试图修改本教程,在我的Android Studio项目中包含一个预建的C库(即不使用实验性的Gradle插件)http://kvurd.com/blog/compiling-a-cpp-library-for-android-with-android-studio/

图书馆本身来自一个不会透露源代码的客户,因此我无法控制构建过程的那一部分,但是他们已经遵循相同的教程。

项目构建,加载库工作和NDK链接(/jni/my-wrapper.c)工作正常,直到我尝试调用我的prebuild头中定义的实际库函数。我收到的错误是:

$ ndk-build
[arm64-v8a] Compile        : my-wrapper <= my-wrapper.c
[arm64-v8a] SharedLibrary  : libmy-wrapper.so
/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/objs/my-wrapper/my-wrapper.o: In function `Java_com_my_project_SignInActivity_CallFunction':
/Users/me/AndroidStudioProjects/MyProject/app/jni/my-wrapper.c:44: undefined reference to `MyFunction'
collect2: error: ld returned 1 exit status
make: *** [/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/libmy-wrapper.so] Error 1

这是我的Android.mk:

LOCAL_PATH := $(call my-dir)

# static library info
include $(CLEAR_VARS)
LOCAL_MODULE := libMyLib
LOCAL_MODULE_FILENAME := libMyLib
LOCAL_SRC_FILES := ../prebuild/libMyLib.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)

# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE    := my-wrapper
LOCAL_SRC_FILES := my-wrapper.c
LOCAL_STATIC_LIBRARIES := libMyLib
include $(BUILD_SHARED_LIBRARY)

和MyLib.h(请注意,foobar()在标题中工作正常,但只要我在my-wrapper.c中调用MyFunction,ndk-build就会失败):< / p>

#include <math.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int MyFunction(some stuff);
int foobar(){return 1;};

最后,my-wrapper.c:

#include <MyLib.h>

jbyte Java_com_my_project_SignInActivity_MyFunction(JNIEnv *env, jobject thiz, some other stuff){

//   return MyFunction(some other stuff which I cast to C types); //linker fails if uncommented

    return foobar(); //works fine
}

1 个答案:

答案 0 :(得分:1)

这是一个C ++错位的名字。您只能使用C ++,而不能使用C语言。

如果你真的需要从C调用它,你可能会这样做:

extern int _Z12MyFunctionP9my_structPhS1_S1_(/* whatever the function args are */);

jbyte Java_com_my_project_SignInActivity_MyFunction(
        JNIEnv *env, jobject thiz, some other stuff) {
    return _Z12MyFunctionP9my_structPhS1_S1_(args);
}

这取决于您调用的代码是否兼容(如果是这种情况,您应该要求客户端将其API构建为extern "C")。

我真的建议您将代码移动到C ++。