如何使用 Android NDK 编译静态库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2943828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:59:21  来源:igfitidea点击:

How to compile a static library using the Android NDK?

androidstatic-librariesandroid-ndk

提问by Reimund

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.

我正在尝试编译一个静态库以在 Android 上使用,但我不知道如何编译它。该库使用标准库(stdio.h 等)和 libxml2。

I am trying to compile using arm-eabi-gcc but I get the following error:

我正在尝试使用 arm-eabi-gcc 进行编译,但出现以下错误:

/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

/cygdrive/c/android-ndk-r4/build/platforms/android-8/arch-x86/usr/include/asm/posix_types.h:15:28: 错误: posix_types_64.h: 没有那个文件或目录

How do I get this to work?

我如何让这个工作?

回答by Reimund

As I understand it, the correct method is to use ndk-buildand not invoking the compiler directly.

据我了解,正确的方法是使用ndk-build而不是直接调用编译器。

In Android.mkyou need to specify a module for each static library you want to compile, and then specify that your shared library should use it.

Android.mk 中,您需要为每个要编译的静态库指定一个模块,然后指定您的共享库应该使用它。

Example of a modified Android.mk file of the hello-jni sample project:

hello-jni 示例项目的修改后的 Android.mk 文件示例:

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)

If you want control over which modules to compile when you run ndk-build you can create create a Application.mkfile (in the same directory as Android.mk) and list all the modules as in the following example:

如果您想在运行 ndk-build 时控制要编译的模块,您可以创建一个Application.mk文件(在与 Android.mk 相同的目录中)并列出所有模块,如下例所示:

APP_MODULES := <module_name_1> <module_name_2> ... <module_name_n>

回答by adelphus

In response to

回应

Can you generate a static library (.a file) without a shared library that uses it?

你能生成一个没有使用它的共享库的静态库(.a 文件)吗?

(which really should have been its own question), the answer is yes.

(这真的应该是它自己的问题),答案是肯定的。

By default, the NDK will only build executables and shared libraries (with their dependencies of course). You can, however, force the NDK to build a standalone static library by explicitly referencing it in your Application.mk.

默认情况下,NDK 只会构建可执行文件和共享库(当然还有它们的依赖项)。但是,您可以通过在 Application.mk 中显式引用 NDK 来强制 NDK 构建一个独立的静态库。

Assuming your static library module is LOCAL_MODULE := libXYZ, add the following line to Application.mk(creating the file in the same folder as your Android.mk if it doesn't exist):

假设您的静态库模块是 LOCAL_MODULE := libXYZ,将以下行添加到Application.mk(如果该文件不存在,则在与您的 Android.mk 相同的文件夹中创建该文件):

APP_MODULES := XYZ

Note the the APP_MODULES value does not include the lib prefix included in your static library module name.

请注意 APP_MODULES 值不包括包含在静态库模块名称中的 lib 前缀。

Alternatively, if you don't want to create an Application.mk, you can specify the value on the command line: ndk-build APP_MODULES=XYZ

或者,如果您不想创建 Application.mk,您可以在命令行上指定值: ndk-build APP_MODULES=XYZ

回答by Lake

A cool trick: if you have an Android.mk file, you can change the build type from:

一个很酷的技巧:如果你有一个 Android.mk 文件,你可以改变构建类型:

include $(BUILD_SHARED_LIBRARY)

to

include $(BUILD_STATIC_LIBRARY)

and .a libraries will be output to the obj/ folder into their respective architectures when you ndk-build the library.

和 .a 库将在您 ndk-build 库时输出到 obj/ 文件夹到它们各自的体系结构中。