的Andr​​oid NDK使用来自内部JNI C $ C $Ç.so库NDK、oid、Andr、so

2023-09-07 14:23:37 作者:哑巴诉爱

我发现了类似的问题,对SO,但他们都不是用相同的工作流程。

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

我在我的项目一个.so库(libcurl的)。该项目建立,但我需要得到举行curl.h在里面JNI我的C code。

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.

下面是我的Andr​​oid.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.h包括命令。我也尝试作为,但没有找到,要么:

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"

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

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:

没有人有任何想法,为什么我不能够得到referece到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:致命错误:卷曲/ curl.h:没有这样的文件或目录

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

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

有了一个明确定义的库安装,这些将在路径中提供毗邻二 - 也就是说,你将有一些curlibrary / lib目录/ libcurl.so和它旁边的curlibrary /有/卷曲/ curl.h

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.

为使这项工作,你将包括目录添加卷曲的路径到编译器命令行,presumably将它添加到你的Andr​​oid.mk

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"

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

要利用包括路径,你提到的code需要的库被尖括号括起来,而不是双引号,即

Or wherever you are keeping it.

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"  //while this specifies a location relative to this source file

在一个更电传操纵夜背景您可能没有真正要使用一个良好定义的安装,而只是一个.so文件(与你的Andr​​oid 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.