> ## Content Index
> Fetch the complete content index at: https://yinguobing.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# JNI函数报错：no implementation found for
- URL: https://yinguobing.com/blog/jnihan-shu-bao-cuo-no-implementation-found-for/
- Published: 2017-02-24T08:39:35.000Z
- Updated: 2022-05-29T08:39:29.000Z
- Description: 一对括号引发的问题。
- Author: 尹国冰
- Tags: Android, C++

如果用Android Studio新建一个支持C++的工程，它会生成一个默认的`native-lib.cpp`的文件，并且定义一个函数`stringFromJNI`。整个代码架构看起来是这个样子：

```
#include <jni.h>
#include <string>
using namespace std;

extern "C"

jstring Java_<someID>_stringFromJNI(JNIEnv *env, jobject) {
    String hello = "Hello from C++!";
    return env->NewStringUTF(hello.c_str());
}

```

我尝试加入第二个函数`intFromJNI`，并将函数代码块放置在第一个函数的后边。这个时候代码看起来是这个样子：

```
#include <jni.h>
#include <string>
using namespace std;

extern "C"

jstring Java_<someID>_stringFromJNI(JNIEnv *env, jobject) {
    String hello = "Hello from C++!";
    return env->NewStringUTF(hello.c_str());
}

jint Java_<someID>_intFromJNI(JNIEnv *env, jobject) {
    return 31415926;
}

```

此时运行会报错：`no implementation found for int Java_<someID>_intFromJNI()...`

Google一番后发现别人是把 `extern "C"` 后的代码放在了大括号`{}`里。于是照猫画虎将两个函数全部放在大括号里，问题解决。最终的代码看起来是这个样子：

```
#include <jni.h>
#include <string>
using namespace std;

extern "C" {
jstring Java_<someID>_stringFromJNI(JNIEnv *env, jobject) {
    String hello = "Hello from C++!";
    return env->NewStringUTF(hello.c_str());
}

jint Java_<someID>_intFromJNI(JNIEnv *env, jobject) {
    return 31415926;
}
}

```

C/C++的基础还不牢固，需要加强。