extern "C" and JNI

C

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.

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
 
A

Alf P. Steinbach

* 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.
 
C

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"?

Thanks again,
cpp
 
A

Alf P. Steinbach

* cppaddict:
Alf,

Thanks for the info.


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).
 
C

cppaddict

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).

Ahh... okay, that makes sense.

Is that the only thing that extern "C" does in general?

Thanks again,
cpp
 
A

Alf P. Steinbach

* cppaddict:
Ahh... okay, that makes sense.

Is that the only thing that extern "C" does in general?

What more it does depends on your compiler and system.

The intention in the standard is to provide C compatibility
in general.

But e.g. with Visual C++ and Windows compilers in general the
machine code calling convention is specified separately, thus
in the generated JNI header file you have macros JNICALL and
JNIEXPORT, to cater for such compilers.
 
J

JKop

First, a sample function:

int Cheese(const std::vector&, double);


A C++ compiler might give that a name like:


Cheese@@c'std::vector#@@double


while a C compiler will give it the name:

Cheese


If you want to make it the latter, then:

extern "C" int Cheese(const std::vector&, double);

-

Why the difference? This is why:

int Cheese(double);
int Cheese(int);


-JKop
 
J

JKop

cppaddict posted:
So this is the reason that C does not support overloaded functions?

More like a consequence of it. Both of those functions will
be:

Cheese
Cheese

so the linker will puke. But in C++, they'll be something
like:

Cheese@@int
Cheese@@double

-JKop
 
D

Default User

cppaddict said:
So this is the reason that C does not support overloaded functions?


It's rather the other way around. C doesn't have overloads, so
name-mangling is not needed.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top