-shared option

Ranjit Mathew rmathew4lists@hotmail.com
Tue Apr 1 07:52:00 GMT 2003


> what's the output file type using gcj -shared option in Windows OS?  It's
> just a library of objective files or a DLL file? I was confused by following
> examples(I don't what's type of *.so because I have no experence on
> UNIX/Linux :( )
> 
> gcj -shared -o swt.so ClassA.o JNIClassB.o ....

On Windows the output is a DLL file but is named "a.exe" by 
default unless you specify "-o foo.dll" explicitly.

However, by default, only a few symbols are exported out of
this DLL, namely:
---------------------------- 8< --------------------------------
LIBRARY     FOO.DLL

EXPORTS
    JNI_CreateJavaVM              = JNI_CreateJavaVM@12
    JNI_GetCreatedJavaVMs         = JNI_GetCreatedJavaVMs@12
    JNI_GetDefaultJavaVMInitArgs  = JNI_GetDefaultJavaVMInitArgs@4
---------------------------- 8< --------------------------------

This means that you would not be able to link against your
"normal" Java methods using this DLL as is.

If you're familiar with Win32 programming, you'd know that to
export more symbols one can either mark the methods/variables
"__declspec(dllexport)" (which translates to 
"__attribute__(("dllexport"))" for GCC) or explicitly write a 
"DEF file" containing the exported symbol definitions.

Method attributes do not work with GCJ (or at least I do not
know how to use them) and I feel it might be a nice idea
to introduce *some* of them to GCJ (though others might
disagree with this).

I tried to make it work with DEF files, but though I was 
able to link without problems, the built executable throws
a NullPointerException on my face straight away... :-(

So it doesn't work (yet), but if you're interested in the 
gory details of how I did it, here are the details:

I have a simple class "Foo" that has a method "bar( )" that
needs to be exported:
---------------------------- 8< --------------------------------
public class Foo {
  public void bar( ) {
    System.out.println( "Hello");
  }
}
---------------------------- 8< --------------------------------

I compile this to an object file:

    gcj -c Foo.java

I now get the symbols that ought to be exported:
---------------------------- 8< --------------------------------
C:\> nm Foo.o |find "T "

    00000000 T __ZN3Foo3barEv
    0000004a T __ZN3FooC1Ev

C:\> nm Foo.o |find "D "

    00000080 D __ZN3Foo6class$E
    00000040 D __ZTVN3FooE
---------------------------- 8< --------------------------------
(getting the defined "text" and "data" symbols)

...and create a DEF file "foo.def" with these symbols:
---------------------------- 8< --------------------------------
EXPORTS
    _ZN3Foo3barEv
    _ZN3FooC1Ev
    _ZN3Foo6class$E
    _ZTVN3FooE
---------------------------- 8< --------------------------------
(Note that the leading underscores have been stripped.)

I now create the DLL using the command:

    gcj -shared -o foo.dll Foo.java foo.def

Linking against this DLL is simple:

    gcj --main=HelloWorld HelloWorld.java foo.dll

...though the created executable directly throws a 
NullPointerException. :-(

I'll have to investigate this a bit more...

Ranjit.



More information about the Java mailing list