假设您有一个正确实现了 Test 类的 first.cpp 文件,您在这里有多个选项。由于无法查看您的 Android.mk,我将浏览所有选项:
将 First.cpp 构建为静态或共享库,并将此库添加到编译 Second.cpp 的模块中。您的 Android.mk 应如下所示:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := firstlib
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_SRC_FILES := first.cpp
include $(BUILD_STATIC_LIBRARY)
如果您希望 First 成为共享库而不是静态库,请将 include $(BUILD_STATIC_LIBRARY) 行更改为:
include $(BUILD_SHARED_LIBRARY)
现在,您的第二个库编译如下:
include $(CLEAR_VARS)
LOCAL_MODULE := second
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_C_INCLUDES += path/to/second.h
LOCAL_SRC_FILES := second.cpp
LOCAL_STATIC_LIBRARIES := firstlib
include $(BUILD_SHARED_LIBRARY)
如果 firstlib 被构建为共享库,您可以通过将 LOCAL_STATIC_LIBRARIES += firstlib 行更改为以下内容来链接它:
LOCAL_SHARED_LIBRARIES += firstlib
作为第二种解决方案,您可以将 first.cpp 构建为第二个库的一部分,这样您就不必担心链接到第一个库。这更像是一种设计选择,以及您希望如何构建您的库:
include $(CLEAR_VARS
LOCAL_MODULE := libtwolib-second
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_C_INCLUDES += path/to/second.h
LOCAL_SRC_FILES := first.cpp
LOCAL_SRC_FILES += second.cpp
include $(BUILD_SHARED_LIBRARY)
最后,您可以在 NDK 目录中的 samples/twolibs 下找到第一种方法的示例。