Generating .dll files for JNI under Windows/Cygwin/GCC

In Windows you can use the Cygwin GCC compiler/linker to produce the .dll files needed for JNI (Java Native Interface) applications. Take for example the JNI HelloWorld example from Sun.

But there seem to be several obstacles. First I got compiler errors (concerning undefined jlong type) and after resolving this I wondered how to produce a Win32 native .dll file.

The solution is as follows (my environment is J2SE 1.4.2-b4, Cygwin GCC 3.3.1):

Changes to JDK headers needed for GCC

(Take from here) GCC doesn’t have a __int64 built-in, and this patch basically uses “long long” instead.

  1. Edit the file /include/win32/jni_md.h, Where   is the installation root (eg., c:/jdk1.4.2).

  2. Replace the segment:

    typedef long jint;
    typedef __int64 jlong;
    typedef signed char jbyte;

    with:

    typedef long jint;
    #ifdef __GNUC__typedef long long jlong;
    #else
    typedef __int64 jlong;
    #endif
    typedef signed char jbyte;

Modify the GCC spec file

This modification is only important if the next step (compiling and linking) produces an linker error “dllcrt2.o not found”.

(Take from here) In the Cygwin shell edit the file “/lib/gcc-lib/i686-pc-cygwin/3.3.1/specs” (replace 3.3.1 with your GCC version). Find the text “dllcrt2″ and replace it with the absolute (Cygwin) path to dllcrt2.o, e.g. “/lib/mingw/dllcrt2″.

Compile and Link the .dll file

(Take from here) Call the compiler:

gcc -L /lib/mingw -Wl,--kill-at -mno-cygwin -shared -I /include/ -I /include/win32/ HelloWorldImpl.c -o hello.dll

Replace with the Cygwin path to your J2SE, e.g. “/cygdrive/c/Java/jdk1.4.2″.

That’s it. Now I get “Hello World!”. Fine.

About these ads

10 Comments »

  1. Anonymous said

    You can also use the MinGW compiler – http://www.mingw.org (MinGW (“Minimalistic GNU for Windows”) refers to a set of runtime headers, used in building a compiler system based on the GNU GCC and binutils projects. – from the website) to make JNI .dlls. I prefer this approach since it doesn’t require installing Cygwin or changing any JDK files. It took me a little while to figure out the syntax with Ant (and requires an Ant library from Sourceforge), but I prefer this approach (since I like having a simple-to-setup build environment). If you want any details, email me at gpayne@artium.com.

    Greg–>

  2. stevpan said

    Thanks a lot, it solves my problem! I’m a java developer under windows. I searched google for quite some time to find this nice site.

  3. Hi, Thanks for pinpointing the problem with the __int64. Though i would recommend the following instead as it does not involve modifying the 3rd party header file.

    in your file before you reference enter the following line
    typedef long long __int64;

  4. MIrribarra said

    Hi!

    Well, solutions look good, but there’s a couple of point about that:

    1. You do not need modify header file, only put:

    -D__int64=”long long”

    To the command to compile.

    2. I can create a .dll, but it’s still have depend of cygwin1.dll… why? I put “-mno-cygwin” and still look for that file.

  5. arun said

    in your article abour generating .dll files for JNI under windows/cygwin/gcc
    you mentioned ” First I got compiler errors (concerning undefined jlong type) and after resolving this ” how did you resolved this problem as i am also facing same problem . can you please guide me my email is arunpushkar@yahoo.com

  6. arun said

    even after copying lines given below it is still giving
    C:/Program Files/Java/jdk1.6.0_10/include/win32/jni_md.h:15:24: warning: extra tokens at end of #ifdef directive
    and

    C:/Program Files/Java/jdk1.6.0_10/include/win32/jni_md.h:17: error: syntax error before “jlong”

    typedef long jint;
    #ifdef __GNUC__typedef long long jlong;
    #else
    typedef __int64 jlong;
    #endif
    typedef signed char jbyte;

  7. Nagy Papa said

    Hi Martin, thanx for posting those misterious cygwin gcc flags. Without this blog, I could have not found it out ever..

  8. Bram said

    Correction (rather obvious if you ask me):

    typedef long jint;
    #ifdef __GNUC__
    typedef long long jlong;
    #else
    typedef __int64 jlong;
    #endif
    typedef signed char jbyte;

  9. […] #split {}#single {}#splitalign {margin-left: auto; margin-right: auto;}#singlealign {margin-left: auto; margin-right: auto;}.linkboxtext {line-height: 1.4em;}.linkboxcontainer {padding: 7px 7px 7px 7px;background-color:#eeeeee;border-color:#000000;border-width:0px; border-style:solid;}.linkboxdisplay {padding: 7px 7px 7px 7px;}.linkboxdisplay td {text-align: center;}.linkboxdisplay a:link {text-decoration: none;}.linkboxdisplay a:hover {text-decoration: underline;} function opensingledropdown() { document.getElementById('singletablelinks').style.display = ''; document.getElementById('singlemouse').style.display = 'none'; } function closesingledropdown() { document.getElementById('singletablelinks').style.display = 'none'; document.getElementById('singlemouse').style.display = ''; } Mac OS X install gcc compiler – Tech-RecipesHow to compile a gcc compiler for another machine which got no c compiler on itWhere is Linux GNU C or gcc compiler installedgcc for WindowsInstalling GCC compiler and GNU Screen on Solaris 10Installing GCC compiler and GNU Screen on Solaris 10GCC for WindowsUsing Tk (and X) from PerlInstalling Compilers (GCC,G++ and Java) on UBUNTUGenerating .dll files for JNI under Windows/Cygwin/GCC […]

  10. Marton said

    Thank you very much, this article came in a really good use!

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: