gpt4 book ai didi

c++ - JNI : java. lang.UnsatisfiedLinkError 错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:22 24 4
gpt4 key购买 nike

我在尝试让 JNI 工作时遇到了一些问题。我有一个 C++ 应用程序,我想在我的 Java 程序中调用这个应用程序的方法。我编写了调用 native 方法的 Java 程序,然后使用 javah 生成头文件。一旦我的 C++ 测试程序被编译成一个库 .so,我就从我的 java 程序中调用这个库。加载System.loadLibrary是可以的,但是他好像找不到里面的方法。我得到一个:

Exception in thread "main" java.lang.UnsatisfiedLinkError: ApiPackets.get_lost_packets()V
at ApiPackets.get_lost_packets(Native Method)
at ApiPackets.main(ApiPackets.java:12)

这是我的 Java 调用 native 方法:

public class ApiPackets {

public native void get_lost_packets();
public native int get_late_packets();
public native int get_out_of_order_packets();

static { System.loadLibrary("ApiPackets");}

public static void main(String[] args) {
ApiPackets api = new ApiPackets();
System.out.println("pass");
api.get_lost_packets();
}
}

这是我的 C++,然后编译成 libApiPackets.so:

#include "ApiPackets.h"
#include <stdio.h>
#include "main_window.h"

JNIEXPORT void JNICALL Java_ApiPackets_get_lost_packets
(JNIEnv * env, jobject obj) {
printf("coucou");
return;
}

JNIEXPORT jdouble JNICALL Java_ApiPackets_get_1late_1packets
(JNIEnv * env, jobject obj) {
jdouble late = mw->priv->current_call->get_late_packets ();
return late;
}

JNIEXPORT jdouble JNICALL Java_ApiPackets_get_1out_1of_1order_1packets
(JNIEnv * env, jobject obj) {
jdouble out_of_order = mw->priv->current_call->get_out_of_order_packets ();
return out_of_order;
}

最佳答案

C++ 函数必须声明为 extern "C"。 (JNI 宏JNIEXPORT 不会这样做,因为它被设计为同时在 C和 C++。)

关于c++ - JNI : java. lang.UnsatisfiedLinkError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com