简体   繁体   中英

Android NDK use .so library from c code inside JNI

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

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.

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)

Here's my c class:

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

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

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"

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:

在此处输入图片说明

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?

Thank you!

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

include "curl/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.

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

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>  //this searches the include path

instead of

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM