如何使用 Android NDK 编译静态库? [英] How to compile a static library using the Android NDK?
问题描述
我正在尝试编译一个静态库以在 Android 上使用,但我不知道如何编译它.该库使用标准库(stdio.h 等)和 libxml2.
I'm trying to compile a static library to use on Android but I can't figure out how to compile it. The library uses standard libraries (stdio.h etc...) and libxml2.
我正在尝试使用 arm-eabi-gcc 进行编译,但出现以下错误:
I am trying to compile using arm-eabi-gcc but I get the following error:
/cygdrive/c/android-ndk-r4/build/platforms/android-8/arch-x86/usr/include/asm/posix_types.h:15:28:错误:posix_types_64.h:没有这样的文件或目录
/cygdrive/c/android-ndk-r4/build/platforms/android-8/arch-x86/usr/include/asm/posix_types.h:15:28: error: posix_types_64.h: No such file or directory
如何让它工作?
推荐答案
据我了解,正确的方法是使用ndk-build,而不是直接调用编译器.
As I understand it, the correct method is to use ndk-build and not invoking the compiler directly.
在Android.mk 中,您需要为每个要编译的静态库指定一个模块,然后指定您的共享库应该使用它.
In Android.mk you need to specify a module for each static library you want to compile, and then specify that your shared library should use it.
hello-jni示例项目修改后的Android.mk文件示例:
Example of a modified Android.mk file of the hello-jni sample project:
LOCAL_PATH := $(call my-dir)
# Define vars for library that will be build statically.
include $(CLEAR_VARS)
LOCAL_MODULE := <module_name>
LOCAL_C_INCLUDES := <header_files_path>
LOCAL_SRC_FILES := <list_of_src_files>
# Optional compiler flags.
LOCAL_LDLIBS = -lz -lm
LOCAL_CFLAGS = -Wall -pedantic -std=c99 -g
include $(BUILD_STATIC_LIBRARY)
# First lib, which will be built statically.
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_STATIC_LIBRARIES := <module_name>
LOCAL_C_INCLUDES := <header_files_path>
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
如果您想在运行 ndk-build 时控制要编译的模块,您可以创建一个 Application.mk 文件(在与 Android.mk 相同的目录中)并将所有模块列为在以下示例中:
If you want control over which modules to compile when you run ndk-build you can create create a Application.mk file (in the same directory as Android.mk) and list all the modules as in the following example:
APP_MODULES := <module_name_1> <module_name_2> ... <module_name_n>
这篇关于如何使用 Android NDK 编译静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!