[英]Unable to call member function of C++ from java using JNI
I followed the instruction given in below said link Call c function from Java and after which I was able to make call to C++ function.我按照下面给出的说明从 Java 调用 c 函数,然后我就可以调用 C++ 函数了。 But now I want to call member function of C++ class from Java.
但是现在我想从 Java 调用 C++ 类的成员函数。 To give a clear picture of it I am citing the scenario below.
为了清楚地说明它,我引用了下面的场景。
JAVA.爪哇。 There is a class called HelloWorld.java and it has native function called
print()
Now using Java's JNI I have create the equivalent header file for HelloWorld.有一个叫做 HelloWorld.java 的类,它有一个叫做
print()
本地函数 现在使用 Java 的 JNI 我已经为 HelloWorld 创建了等效的头文件。 After which i wrote the implemenation of this header file in the HelloWorld.cpp之后我在 HelloWorld.cpp 中编写了这个头文件的实现
Now from "HelloWorld.cpp" I want to call the member function of "rectangle.cpp" for which I have created object of "rectangle" and called its corresponding function.现在从“HelloWorld.cpp”我想调用“rectangle.cpp”的成员函数,我已经为其创建了“rectangle”对象并调用了它的相应函数。 But upon compilation of code it give me an error called "unresolved external symbol".
但是在编译代码时,它给了我一个名为“未解析的外部符号”的错误。 On the contrary when I write all the implemenation in corresponding header file "rectangle.cpp" that is in rectangle.h, the code compiles well and it gave me desired result.
相反,当我在矩形.h 中的相应头文件“rectangle.cpp”中编写所有实现时,代码编译得很好,它给了我想要的结果。 My Question, Isnt there a way, following which i can call the member function of .cpp class not its corresponding header file funcion.
我的问题,是不是有一种方法,我可以调用 .cpp 类的成员函数而不是其相应的头文件函数。
Below is the code snippet::下面是代码片段::
HelloWorld.java你好世界
class HelloWorld {
private native void print();
private native void multiply();
public static void main(String[] args) {
new HelloWorld().print();
new HelloWorld().multiply();
}
static {
System.loadLibrary("HelloWorld");
} }
HelloWorld.h你好世界
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: print
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *, jobject);
/*
* Class: HelloWorld
* Method: multiply
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_multiply
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
HelloWorld.cpp HelloWorld.cpp
#include <jni.h>
#include <stdio.h>
#include "rectangle.h"
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject
obj)
{
// printf("Hello World!dfdfdfdf\n");
// return;
Rectangle rectangle;
rectangle.set_values(3,4);
printf("Area of Rectangle %d", rectangle.get_values());
}
JNIEXPORT void JNICALL Java_HelloWorld_multiply
(JNIEnv *, jobject)
{
//printf("dila ");
}
rectangle.h矩形.h
#ifndef GUARD_SNAKE_H
#define GUARD_SNAKE_H
class Rectangle{
public:
Rectangle(void);
// ~Rectangle();
void set_values(int x,int y);
int get_values();
private:
int width;
int height;
};
#endif
rectangle.cpp矩形.cpp
// classes example
#include "rectangle.h"
Rectangle::Rectangle(void)
{
}
void Rectangle::set_values(int x, int y)
{
width = x;
height = y;
}
int Rectangle::get_values()
{
return width*height;
}
Issue associated with aforewritten code: When I wrote all the implementation of "rectangle" in its header file "rectangle.h", it gave me the desired result.与上述代码相关的问题:当我在头文件“rectangle.h”中编写“rectangle”的所有实现时,它给了我想要的结果。 The problem is "rectangle" object created on "HelloWorld.cpp" file is not referring to "rectangle.cpp".
问题是在“HelloWorld.cpp”文件上创建的“rectangle”对象不是指“rectangle.cpp”。 That's why when i compiled and run it, it gave me the "unresolved external symbol" exception , which means, compiler or debugger cannot find implemenation for the function defined in "rectangel.h" file.
这就是为什么当我编译并运行它时,它给了我“未解析的外部符号”异常,这意味着编译器或调试器找不到“rectangel.h”文件中定义的函数的实现。 Is there any way to solve this problem.. Please Help.
有什么办法可以解决这个问题..请帮忙。
In case you want to use multiple sources, make sure to:如果您想使用多个来源,请确保:
You can find very simple samples here:您可以在这里找到非常简单的示例:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo021 https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023 https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo021 https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.