Static linking of JNI code - HOW?

Erik Poupaert erik.poupaert@chello.be
Mon Jun 16 17:35:00 GMT 2003


> > We are creating an application with GCJ and we wish to statically
> > link our JNI code to bypass the whole shared library mess.  I have
> > been able to statically link in my JNI code, but the runtime is
> > still looking for the code in a lib

The following is an example of how I link statically with GCJ. Note that
you must watch out for a few things.

Be specifically careful to embed jni-archives into a whole-archive
wrapper. The linker cannot know that you are going to use any method in
the archive, because jni uses dynamic lookups to resolve symbols (where
cni does not).


gcj -export-dynamic \
         -o ../bld-app-showtimes/binlin32/showtimes \
             --main=com.cinematic.showtimes.swtgui.ShellMain \
             -Xlinker --start-group \   
../bld-app-showtimes/arclin32/libcom-cinematic-showtimes-swtgui-0-8.a \ 
../bld-app-showtimes/arclin32/libcom-cinematic-showtimes-model-0-7.a \  
../bld-app-showtimes/arclin32/liborg-freestyler-cj-date-0-7.a \         
../bld-app-showtimes/arclin32/libcom-cinematic-showtimes-xmlrpc-0-8.a \ 
../bld-app-showtimes/arclin32/libcom-mysql-jdbc-3-1-nightly-20030316.a \
              ../bld-app-showtimes/arclin32/liborg-apache-xmlrpc-1-1.a \
              ../bld-app-showtimes/arclin32/liborg-xml-sax-1-1.a \      
        ../bld-app-showtimes/arclin32/libuk-co-wilson-xml-1-6.a \       
../bld-app-showtimes/arclin32/liborg-freestyler-comlinsys-model-0-7.a \ 
../bld-app-showtimes/arclin32/liborg-eclipse-swt-lin32-2-132.a \        
       -Wl,--whole-archive
../bld-app-showtimes/arclin32/liborg-eclipse-swt-lin32-jni-2-132.a
-Wl,--no-whole-archive  \              
../bld-app-showtimes/arclin32/liborg-freestyler-swtextra-0-7.a \        
    -Xlinker --end-group \           
../bld-app-showtimes/reslin32/com/cinematic/showtimes/swtgui/SIGNATURE.
gif.o \           
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_NEW_LINE.bmp
.o \           
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_NORMAL.bmp.o
\           
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_SELECTED.bmp
.o \           
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_DOWN.ico.o \ 
         
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_UP.ico.o \   
       
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_HIGHLIGHT.bm
p.o \           
../bld-app-showtimes/reslin32/org/freestyler/swtextra/GRID_EDITING.bmp.o
\             -lgtk-x11-2.0 \
             -lgdk-x11-2.0 \
             -latk-1.0 \
             -lgdk_pixbuf-2.0 \
             -lm \
             -lpangoxft-1.0 \
             -lpangox-1.0 \
             -lpango-1.0 \
             -lgobject-2.0 \
             -lgmodule-2.0 \
             -ldl \
             -lglib-2.0 \
             -lgthread-2.0 \
             -lpthread

The linking instructions were generated by my little tool called
"buildmodelartist". It integrates all comments and instructions that
people on this list have given to me with regards to statical linking.
Thanks everybody.

The buildmodel recursively reads other buildmodels
(mentioned in "archive dependencies"). The "aredir" buildmodels are
just redirectors, that will redirect to the builmodel for a
particular version, dependent on the target platform (win32 or lin32)

The root buildmodel is:

buildmodel com-cinematic-showtimes-swtgui-0-8;

generators
{
	swtmodelartist src/com/cinematic/showtimes/showtimes.swtmodel;
}

entry points
{
	showtimes com.cinematic.showtimes.swtgui.ShellMain BRIDGE.ICO;
}

archive dependencies
{
	aredir-com-cinematic-showtimes-model;
	aredir-com-cinematic-showtimes-xmlrpc;
	aredir-org-freestyler-cj-date;
	aredir-org-apache-xmlrpc;
	aredir-org-eclipse-swt;
	aredir-org-freestyler-swtextra;
	aredir-org-freestyler-comlinsys-model;
}

The "generators" thing, is another story. It causes buildmodelartist to
invoke source code generators.

As soon as I have the time to document this little tool, I'll make it
publicly available under the GPL. Even if it doesn't apply to your
situation, you could deduct the gist of how to link statically on
windows and on linux with GCJ3.3.



More information about the Java mailing list