[738] in java-interest
Re: how to call native C++ code?
daemon@ATHENA.MIT.EDU (Jason Weiler)
Mon Jul 17 23:09:45 1995
Date: Mon, 17 Jul 1995 18:25:42 -0700
From: weilerj@std.teradyne.com (Jason Weiler)
To: java-interest@java.Eng.Sun.COM, jsw@cs.brown.edu
>
> Has anyone been able to implement native code in C++, or in C that
> calls C++ code? Everything compiles allright, but then when I go to
> run it I get a
>
> /pro/java/java-alpha3/bin/java demo.Demo
> java.lang.UnsatisfiedLinkException printhello
> at demo.Demo.<init>(Demo.java:8)
> at demo.Demo.main(Demo.java:13)
>
> If anyone successfully done this, please email me and tell me how you
> did it.
>
> thanks,
> jeff
> -
Yes, I have been able to accomplish this.
First and foremost, you need C-linkage. In other words, you need to
declare all your function prototypes within the confines of
extern "C" { pre-decs }
This is because of the C++ function-name mangling.
if you use "nm -R <libfile> | grep <function_name>" you'll see that
the name has had some garbage tacked on to the end by the C++ compiler.
This is simply to handle function overloading.
(sorry if this was entirely obvious to anyone, I don't mean to preach to the
choir :-)
You don't need to write C-code that calls C++ code, but something I
ran into was a typing conflict with bool_t. You may or may not
hit this wall, but I hurdled it with some simple wrapper functions.
Good luck!
Jason Weiler
<weilerj@std.teradyne.com>
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com