外部“C”和JNI [英] extern "C" and JNI

查看:63
本文介绍了外部“C”和JNI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javah生成的JNI头文件中,当

时,

''extern" C"''在本机方法周围插入了什么? " CPLUSPLUS"定义。


我认为你只需要externC当cplusplus没有定义
时,即当你使用C而不是C ++时。


我想在C ++中实现我的原生方法。我可以这样做吗?

他们被externC包裹?如果是这样,externC究竟是什么?
在做什么?


感谢您的任何澄清,

cpp

以下是JNI头文件的示例,如果以上内容不清楚:


/ *请勿编辑此文件 - 它是机器生成的* /

#include< jni.h>

/ * HelloWorld类的标题* /


#ifndef _Included_HelloWorld

#define _Included_HelloWorld

#ifdef __cplusplus

extern" C" {

#endif

/ *

*等级:HelloWorld

*方法:displayHelloWorld

*签名:()V

* /

JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld

(JNIEnv *,jobject);


#ifdef __cplusplus

}

#endif

#endif

解决方案

* cppaddict:


在javah生成的JNI头文件中,
''发生了什么当
cplusplus时,插入本地方法的externC''被定义为。


JNI是一个兼容C的界面。


C的链接名称有一个事实上的标准,在C ++中没有,

所以JNI使用C绑定。


实际上JNI的一部分是以Windows COM技术为蓝本的(有

三标准C ++ vtable函数指针的槽,但

它没有任何用处,所以不要试图利用它......

我想在C ++中实现我的原生方法。当他们被外部C包裹时,我可以这样做吗?


是。


如果是这样,那么外部C究竟是什么?在做什么?




它提供C链接,例如避免C ++编译器特定的名称

mangling。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Alf,


感谢您的信息。

C的链接名称有一个事实上的标准,在C ++中没有这样的标准,所以JNI使用C绑定。




你能解释一下JNI使用C绑定意味着什么吗?

再次感谢,

cpp


* cppaddict:

Alf,

感谢您的信息。

C的链接名称有一个事实上的标准,在C ++中没有这样的标识,所以JNI使用C绑定。

你能解释一下JNI使用C绑定意味着什么吗?




基本上它意味着功能


void Java_HelloWorld_displayHelloWorld();


显示为_Java_HelloWorld_displayHelloWorld到链接器(领先

下划线),而不是更具装饰性和编译器特定的名称。


当Java运行时在动态中查找函数名称时库它

这样做的名称,所以它需要一个简单,一致的链接命名方案

(除了JNI命名约定)。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


In JNI header files generated by javah, what is going on with the
''extern "C"'' which is inserted around the native method when
"cplusplus" is defined.

I would think you would only need extern "C" when cplusplus wasn''t
defined, ie, when you were using C and not C++.

I want to implement my native methods in C++. Can I do this when
they''re wrapped by extern "C"? If so, what exactly is extern "C"
doing?

Thanks for any clarification,
cpp

Here is sample JNI header file, if the above wasn''t clear:

/* 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: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

解决方案

* cppaddict:


In JNI header files generated by javah, what is going on with the
''extern "C"'' which is inserted around the native method when
"cplusplus" is defined.
JNI is a C-compatible interface.

There is a de-facto standard for linkage names for C, no such in C++,
so JNI uses the C binding.

In fact part of JNI is modelled on Windows COM technology (there are
three "slots" for the COM standard C++ vtable function pointers), but
it''s not useful in any way, so don''t try to take advantage of that...

I want to implement my native methods in C++. Can I do this when
they''re wrapped by extern "C"?
Yes.

If so, what exactly is extern "C" doing?



It provides C linkage such as avoiding C++ compiler-specific name
mangling.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf,

Thanks for the info.

There is a de-facto standard for linkage names for C, no such in C++,
so JNI uses the C binding.



Could you explain what it means for JNI to use "C binding"?

Thanks again,
cpp


* cppaddict:

Alf,

Thanks for the info.

There is a de-facto standard for linkage names for C, no such in C++,
so JNI uses the C binding.



Could you explain what it means for JNI to use "C binding"?



Essentially it means that the function

void Java_HelloWorld_displayHelloWorld();

appears as "_Java_HelloWorld_displayHelloWorld" to the linker (leading
underscore), instead of a more ornamented and compiler-specific name.

When the Java runtime looks up function names in your dynamic library it
does so by name, so it needs a simple, consistent linkage naming scheme
(in addition to JNI naming convention).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于外部“C”和JNI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆