(Android)如何使用动态链接库构建本机c hello世界?

时间:2013-04-25 05:01:45

标签: android native dynamic-linking

我想用动态链接库制作本机c程序,所以我将这些文件放在Android / development / hello /:hello.c,myprint.h,myprint.c中,main()在hello中。 c,Android.mk是:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := myprint
LOCAL_SRC_FILES := myprint.c
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := helloworld
LOCAL_SRC_FILES := hello.c
LOCAL_SHARED_LIBRARIES := myprint
include $(BUILD_EXECUTABLE)

然后我制作helloworld,输出信息为:

target thumb C: helloworld <= development/hello/hello.c
target thumb C: myprint <= development/hello/myprint.c
target SharedLib: myprint (out/target/product/generic/obj/SHARED_LIBRARIES/myprint_intermediates/LINKED/myprint.so)
target Symbolic: myprint (out/target/product/generic/symbols/system/lib/myprint.so)
target Strip: myprint (out/target/product/generic/obj/lib/myprint.so)
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
/home/neil/dev/google_422/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin/../lib/gcc/arm-linux-androideabi/4.7/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmyprint.so
development/hello/hello.c:5: error: undefined reference to 'myprint'
collect2: error: ld returned 1 exit status

请帮帮我......

我的原生代码是:

// hello.c
#include "myprint.h"
int main()
{
    myprint();
    return 0;
}

// myprint.h
void myprint();

// myprint.c
#include <stdio.h>
void myprint()
{
    printf("myprint...\n");
}

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为您可能无法编译这两个文件,因此无法找到/链接该函数的声明。

试试这个

gcc headerfile.c mainfile.c