Android NDK使用.so库从JNI里面的c代码 [英] Android NDK use .so library from c code inside JNI

查看:280
本文介绍了Android NDK使用.so库从JNI里面的c代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上发现了类似的问题,但都没有使用相同的工作流程。

I found similar questions on SO but none of them were with the same workflow.

我在我的项目中有一个.so库(libcurl)。

I have a .so library (libcurl) in my project. The project builds but I need to get a hold of curl.h in my c code inside JNI.

这是我的Android.mk文件:

Here's my Android.mk file:

LOCAL_PATH:= $(call my-dir)

LIBS_PATH := libs/$(TARGET_ARCH_ABI)

include $(CLEAR_VARS)
LOCAL_MODULE := libcurl                     
LOCAL_SRC_FILES := $(LIBS_PATH)/libcurl.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := testLib
LOCAL_SRC_FILES := lib.c

LOCAL_SHARED_LIBRARIES += libcurl

include $(BUILD_SHARED_LIBRARY)

这是我的c类:

#include "curl/curl.h"
#include "lib.h"

JNIEXPORT jint JNICALL Java_com_example_test_1libcurlandroidlib_Lib_getTestNumber
  (JNIEnv *env , jclass clazz)
{
    return 99;
}

问题是使用curl / curl.hinclude命令。我也尝试过,但它没有找到它:

The issue is with the "curl/curl.h" include command. I have also tried as but it does not find it either:

jni/lib.c:2:23: fatal error: curl/curl.h: No such file or directory
#include "curl/curl.h"

我有我的libcurl.so文件在JNI文件夹内的lib文件夹内,在构建时生成相同(我认为)文件到应用程序根目录下的libs文件夹:

I have my libcurl.so file inside a lib folder inside the JNI folder, which at build time generates the same (I think) file into the libs folder at the root of the app:

>

有没有人知道为什么我不能得到一个curl.h的引用,或者我必须做什么才能保持这个库?

Does anyone have any idea why I am not able to get a referece to curl.h, or what I have to do to get a hold of this library?

谢谢!

推荐答案


jni / lib.c:2:23:fatal error:curl / curl.h:没有此类文件或目录

jni/lib.c:2:23: fatal error: curl/curl.h: No such file or directory

为了使用这个库,你不仅需要编译的.so文件,还需要一组通常由头文件提供的函数原型(也许是数据类型定义)。

To make use of this library, you need not only the compiled .so file, but also a set of function prototypes (and perhaps data type definitions) customarily provided by a header file.

使用定义良好的库安装,这些将在二进制文件旁边的路径中提供 - 即,您将有一些curlibrary /lib/libcurl.so和其旁边的curlibrary / include / curl / curl.h

With a well-defined library installation, these would be provided in a path adjacent to the binary - ie, you would have some "curlibrary/lib/libcurl.so" and next to it a "curlibrary/include/curl/curl.h"

为了使这个工作,你需要添加curl的路径include目录到你的编译器命令行,可能是通过将它添加到你的Android.mk

To make that work, you would add the path of curl's include directory to your compiler command line, presumably by adding it to your Android.mk

LOCAL_C_INCLUDES := curlibrary/include

或任何保存它的地方。

使用include路径,在代码中对库的引用需要括在尖括号中,而不是双引号,即

To make use of the include path, your references to the library in code need to be enclosed in angle brackets, not double quotes, ie

#include <curl/curl.h>  //this searches the include path

而不是

#include "curl/curl.h"  //while this specifies a location relative to this source file

在一个更加飞越的上下文中,您可能没有真正定义安装,而只是一个.so文件(希望与您的Android ABI),以及您提取或甚至重新创建的头文件。在这种情况下,你可能更偶然地在项目源中的某处折腾curl.h,并通过特定的引用路径包含它,因为你在尝试做。如果路径是正确的,它会工作 - 但它打破了清洁的设计层次结构,并且如果卷曲的api在未来改变,可能导致混乱。

In a more fly-by-night context you may not really have a well-defined installation, but simply a .so file (hopefully compatible with your Android ABI) that you want to use, and a header file that you have either extracted or even re-created. In that case, you might more haphazardly toss "curl.h" somewhere in your project source, and include it via a specific quoted path as you were trying to do. Provided that path is correct, it will work - but it breaks the clean hierarchy of design, and could cause confusion if the api of curl ever changes in the future.

这篇关于Android NDK使用.so库从JNI里面的c代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆