在這種情況下,您需要在建構 foo-user 模組時提供標頭,及其指向編譯器的 include 路徑。如要完成這項工作,有個簡單的做法是在預建模組定義中使用匯出值。例如,只要標頭 foo.h 位於與預建模組相關聯的 include 目錄下,您就可以按照以下方式對其進行宣告:
include $(CLEAR_VARS)LOCAL_MODULE:=foo-prebuilt
LOCAL_SRC_FILES:=libfoo.so
LOCAL_EXPORT_C_INCLUDES:=$(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
此處的 LOCAL_EXPORT_C_INCLUDES 定義可確保建構系統匯出指向預建程式庫 include 目錄的路徑,並針對依賴預建程式庫的模組將這個路徑附加到 LOCAL_C_INCLUDES 值的開頭。
include $(CLEAR_VARS)LOCAL_MODULE:=foo-prebuilt
LOCAL_SRC_FILES:=$(TARGET_ARCH_ABI)/libfoo.so
LOCAL_EXPORT_C_INCLUDES:=$(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
如果您已將 armeabi 指定為 TARGET_ARCH_ABI 的值,建構系統便會使用 armeabi 目錄中的 libfoo.so 版本。如果您已將 x86 指定為 TARGET_ARCH_ABI 的值,建構系統便會使用 x86 目錄中的版本。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2024-08-22 (世界標準時間)。"],[],[],null,["# Use prebuilt libraries\n\nThe NDK supports the use of prebuilt libraries, both static and shared. There\nare two principal use cases for this functionality:\n\n- Distributing your own libraries to third-party NDK developers without distributing your sources.\n- Using a prebuilt version of your own libraries to speed up your build.\n\nThis page explains how to use prebuilt libraries.\n\nDeclare a prebuilt library\n--------------------------\n\nYou must declare each prebuilt library you use as an independent module. To do\nso, perform the following steps:\n\n1. Give the module a name. This name does not need to be the same as that of the prebuilt library, itself.\n2. In the module's [Android.mk](/ndk/guides/android_mk) file, assign to `LOCAL_SRC_FILES` the path to\n the prebuilt library you are providing. Specify the path relative to the\n value of your `LOCAL_PATH` variable.\n\n | **Note:** You must make sure to select the version of your prebuilt library appropriate to your target ABI. For more information on ensuring library support for ABIs, see [Select ABIs for prebuilt libraries](#sa).\n3. Include `PREBUILT_SHARED_LIBRARY` or `PREBUILT_STATIC_LIBRARY`, depending on\n whether you are using a shared (`.so`) or static (`.a`) library.\n\nHere is a trivial example that assumes the prebuilt library `libfoo.so` resides\nin the same directory as the [Android.mk](/ndk/guides/android_mk) file that describes it. \n\n LOCAL_PATH := $(call my-dir)\n\n include $(CLEAR_VARS)\n LOCAL_MODULE := foo-prebuilt\n LOCAL_SRC_FILES := libfoo.so\n include $(PREBUILT_SHARED_LIBRARY)\n\nIn this example, the name of the module is the same as that of the prebuilt\nlibrary.\n\nThe build system places a copy of your prebuilt shared library into\n`$PROJECT/obj/local`, and another copy, stripped of debug information, into\n`$PROJECT/libs/\u003cabi\u003e`. Here, `$PROJECT` is the root directory of your project.\n\nReference the prebuilt library from other modules\n-------------------------------------------------\n\nTo reference a prebuilt library from other modules, specify its name as the\nvalue of the `LOCAL_STATIC_LIBRARIES` or `LOCAL_SHARED_LIBRARIES` variable in\nthe [Android.mk](/ndk/guides/android_mk) files associated with those other modules.\n\nFor example, the description of a module using `libfoo.so` might be as follows: \n\n include $(CLEAR_VARS)\n LOCAL_MODULE := foo-user\n LOCAL_SRC_FILES := foo-user.c\n LOCAL_SHARED_LIBRARIES := foo-prebuilt\n include $(BUILD_SHARED_LIBRARY)\n\nHere, `LOCAL_MODULE` is the name of the module referring to the prebuilt;\n`LOCAL_SHARED_LIBRARIES` is the name of the prebuilt, itself.\n\nExport headers for prebuilt libraries\n-------------------------------------\n\nThe code in `foo-user.c` depends on specific declarations that normally reside\nin a header file, such as `foo.h`, distributed with the prebuilt library. For\nexample, `foo-user.c` might have a line like the following: \n\n #include \u003cfoo.h\u003e\n\nIn such a case, you need to provide the header and its include path to the\ncompiler when you build the `foo-user` module. A simple way to accomplish this\ntask is to use exports in the prebuilt module definition. For example, as long\nas header `foo.h` is located under the `include` directory associated with the\nprebuilt module, you can declare it as follows: \n\n include $(CLEAR_VARS)\n LOCAL_MODULE := foo-prebuilt\n LOCAL_SRC_FILES := libfoo.so\n LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include\n include $(PREBUILT_SHARED_LIBRARY)\n\nThe `LOCAL_EXPORT_C_INCLUDES` definition here ensures that the build system\nexports the path to the prebuilt library's `include` directory, prepending that\npath onto the value of the `LOCAL_C_INCLUDES` for the module dependent on it.\n\nThis operation allows the build system to find the necessary headers.\n\nDebug prebuilt libraries\n------------------------\n\nWe recommend that you provide prebuilt shared libraries containing debug\nsymbols. The NDK build system always strips the symbols from the version of the\nlibrary that it installs into `$PROJECT/libs/\u003cabi\u003e/`, but you can use the debug\nversion for debugging with `ndk-gdb`.\n\nSelect ABIs for prebuilt libraries\n----------------------------------\n\nYou must make sure to select the right version of your prebuilt shared library\nfor your targeted ABI. The [`TARGET_ARCH_ABI`](/ndk/guides/android_mk#taa)\nvariable in the [Android.mk](/ndk/guides/android_mk) file can point the build system at the appropriate\nversion of the library.\n\nFor example, assume that your project contains two versions of library\n`libfoo.so`: \n\n armeabi/libfoo.so\n x86/libfoo.so\n\nThe following snippet shows how to use `TARGET_ARCH_ABI` so that the build\nsystem selects the appropriate version of the library: \n\n include $(CLEAR_VARS)\n LOCAL_MODULE := foo-prebuilt\n LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.so\n LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include\n include $(PREBUILT_SHARED_LIBRARY)\n\nIf you have specified `armeabi` as the value of `TARGET_ARCH_ABI`, the build\nsystem uses the version of `libfoo.so` located in the `armeabi` directory. If\nyou have specified `x86` as the value `TARGET_ARCH_ABI`, the build system uses\nthe version in the `x86` directory."]]