小编Sam*_*...的帖子

Android - 如何加载共享库?

我创建了最简单的EXECUTABLE和SHARED_LIBRARY.如果不更改LD_LIBRARY_PATH,则不会加载SHARED_LIBRARY:

# ./hello
./hello
link_image[1995]: failed to link ./hello
CANNOT LINK EXECUTABLE

# LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./hello
Hello, world!
Run Code Online (Sandbox Code Playgroud)

以下所有代码:

first.h

#ifndef FIRST_H
#define FIRST_H

extern int first(int x, int y);

#endif /* FIRST_H */
Run Code Online (Sandbox Code Playgroud)

first.c

#include "first.h"

int first( int x, int y ) {
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

你好ç

#include <stdio.h>
#include "first.h"

int main( int argc, char **argv ) {
    printf( "Hello, world!\n" );
    first( 1000, 24 );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE    := first
LOCAL_SRC_FILES …
Run Code Online (Sandbox Code Playgroud)

android shared-libraries android-ndk

9
推荐指数
1
解决办法
1万
查看次数

用于获取/释放资源的JNI C++模板

从C++代码获取/释放JNI资源的推荐模板集或库是什么?

"坏"的例子:

//C++ code
extern "C"
JNIEXPORT void JNICALL Java_ClassName_MethodName
  (JNIEnv *env, jobject obj, jstring javaString)
{
    //Get the native string from javaString
    const char *nativeString = env->GetStringUTFChars(javaString, 0);

    //Do something with the nativeString

    //DON'T FORGET THIS LINE!!!
    env->ReleaseStringUTFChars(javaString, nativeString);
}
Run Code Online (Sandbox Code Playgroud)

显然,每个人都使用模板集,而不是上面的代码.

对于jstring,当对象超出范围时,它调用GetStringUTFChars获取资源,释放ReleaseStringUTFChars.

必须与auto_ptr模板类似,但为JNI量身定制,例如为jstring调用GetStringUTFChars/ReleaseStringUTFChars.

c++ java-native-interface templates release

3
推荐指数
1
解决办法
1519
查看次数