Connecting Tech Pros Worldwide Forums | Help | Site Map

extern "C" and JNI

cppaddict
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Alf P. Steinbach
Guest
 
Posts: n/a
#2: Jul 22 '05

re: extern "C" and JNI


* cppaddict:[color=blue]
>
> 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.[/color]

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

[color=blue]
> I want to implement my native methods in C++. Can I do this when
> they're wrapped by extern "C"?[/color]

Yes.

[color=blue]
> If so, what exactly is extern "C" doing?[/color]

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?
cppaddict
Guest
 
Posts: n/a
#3: Jul 22 '05

re: extern "C" and JNI


Alf,

Thanks for the info.
[color=blue]
>There is a de-facto standard for linkage names for C, no such in C++,
>so JNI uses the C binding.[/color]

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

Thanks again,
cpp
Alf P. Steinbach
Guest
 
Posts: n/a
#4: Jul 22 '05

re: extern "C" and JNI


* cppaddict:[color=blue]
> Alf,
>
> Thanks for the info.
>[color=green]
> >There is a de-facto standard for linkage names for C, no such in C++,
> >so JNI uses the C binding.[/color]
>
> Could you explain what it means for JNI to use "C binding"?[/color]

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?
cppaddict
Guest
 
Posts: n/a
#5: Jul 22 '05

re: extern "C" and JNI


[color=blue]
>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).[/color]

Ahh... okay, that makes sense.

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

Thanks again,
cpp
Alf P. Steinbach
Guest
 
Posts: n/a
#6: Jul 22 '05

re: extern "C" and JNI


* cppaddict:[color=blue]
>[color=green]
> >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).[/color]
>
> Ahh... okay, that makes sense.
>
> Is that the only thing that extern "C" does in general?[/color]

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.

--
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?
JKop
Guest
 
Posts: n/a
#7: Jul 22 '05

re: extern "C" and JNI


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
cppaddict
Guest
 
Posts: n/a
#8: Jul 22 '05

re: extern "C" and JNI


[color=blue]
>Why the difference? This is why:
>
>int Cheese(double);
>int Cheese(int);[/color]

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

JKop
Guest
 
Posts: n/a
#9: Jul 22 '05

re: extern "C" and JNI


cppaddict posted:
[color=blue]
>[color=green]
>>Why the difference? This is why:
>>
>>int Cheese(double); int Cheese(int);[/color]
>
> So this is the reason that C does not support overloaded[/color]
functions?[color=blue]
>
>[/color]

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
Default User
Guest
 
Posts: n/a
#10: Jul 22 '05

re: extern "C" and JNI


cppaddict wrote:[color=blue]
>[color=green]
> >Why the difference? This is why:
> >
> >int Cheese(double);
> >int Cheese(int);[/color]
>
> So this is the reason that C does not support overloaded functions?[/color]


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



Brian Rodenborn
Closed Thread


Similar C / C++ bytes