如何在单个 android.mk 文件中包含多个 .c 文件?

发布于 2025-01-11 09:03:16 字数 555 浏览 4 评论 0原文

我有 2 个 .c 文件 hello.c 和 world.c 我正在使用 ndk-build。我看到 .so 文件名实际上是“LOCAL_MODULE”的名称。 我如何将这两个文件作为构建的一部分包含在内?

Android.mk 文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := hello.c / world.c
include $(BUILD_SHARED_LIBRARY)

当我尝试访问任一 c 文件中的方法时,出现以下错误。

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip 
file.......] couldn't find "libhello.so" at 
java.lang.Runtime.loadLibrary0(Runtime.java:1067)

不知道我应该如何处理这个问题。

I have 2 .c files hello.c and world.c I'm using ndk-build. I see that the .so file name is actually the name of the "LOCAL_MODULE".
How would I include both the files as a part of the build?

Android.mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := hello.c / world.c
include $(BUILD_SHARED_LIBRARY)

I'm getting the below error when I try to access methods in either of the c file.

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip 
file.......] couldn't find "libhello.so" at 
java.lang.Runtime.loadLibrary0(Runtime.java:1067)

Not sure how I should approach this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2025-01-18 09:03:16

假设您有两个名为 native 和 static 的 .c 文件,
cmakelist.txt 文件的结构如下所示

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native 
 library.

  cmake_minimum_required(VERSION 3.10.2)

 # Declares and names the project.

 project("native")

 # Creates and names a library, sets it as either STATIC
 # or SHARED, and provides the relative paths to its source code. 
 # You can define multiple libraries, and CMake builds them for you.
  # Gradle automatically packages shared libraries with your APK.

 add_library( # Sets the name of the library.
    native

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).

    native.cpp)




    add_library( # Sets the name of the library.
    static

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).

    static.cpp)





 # Searches for a specified prebuilt library and stores the path as a
  # variable. Because CMake includes system libraries in the search path by
 # default, you only need to specify the name of the public NDK library
  # you want to add. CMake verifies that the library exists before
  # completing its b uild.

 find_library( # Sets the name of the path variable.
    log-lib

    # Specifies the name of the NDK library that
    # you want CMake to locate.
    log)

  # Specifies libraries CMake should link to your target library. You
  # can link multiple libraries, such as libraries you define in this
  # build script, prebuilt third-party libraries, or system libraries.

  target_link_libraries( # Specifies the target library.
    native
    static
    # Links the target library to the log library
    # included in the NDK.
    ${log-lib})

Assuming you have two .c files named native and static,
the structure of the cmakelist.txt file will look like this

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native 
 library.

  cmake_minimum_required(VERSION 3.10.2)

 # Declares and names the project.

 project("native")

 # Creates and names a library, sets it as either STATIC
 # or SHARED, and provides the relative paths to its source code. 
 # You can define multiple libraries, and CMake builds them for you.
  # Gradle automatically packages shared libraries with your APK.

 add_library( # Sets the name of the library.
    native

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).

    native.cpp)




    add_library( # Sets the name of the library.
    static

    # Sets the library as a shared library.
    SHARED

    # Provides a relative path to your source file(s).

    static.cpp)





 # Searches for a specified prebuilt library and stores the path as a
  # variable. Because CMake includes system libraries in the search path by
 # default, you only need to specify the name of the public NDK library
  # you want to add. CMake verifies that the library exists before
  # completing its b uild.

 find_library( # Sets the name of the path variable.
    log-lib

    # Specifies the name of the NDK library that
    # you want CMake to locate.
    log)

  # Specifies libraries CMake should link to your target library. You
  # can link multiple libraries, such as libraries you define in this
  # build script, prebuilt third-party libraries, or system libraries.

  target_link_libraries( # Specifies the target library.
    native
    static
    # Links the target library to the log library
    # included in the NDK.
    ${log-lib})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文