[Mingw-users] mingw32 and jdk/jni

Back to archive index
Keith Marshall keith****@users*****
Wed Sep 23 06:20:24 JST 2020


On 22/09/2020 05:11, Paul Edwards wrote:
> using JNI. I am getting an error about not being
> able to find _imp__JNI_CreateJavaVM at 12
> 
> I have got conflicting information from the web
> as to whether the jvm.lib that is supplied in the
> JDK should work or not. Some information says
> that it won't work, because the function is
> stdcall and mingw32 does not prefix the function
> name with an underscore, while MSVC does.

Not true.  Both mingw32 and MSVC 32-bit prepend one extra underscore to
the symbol name, as it appears in C source code, to get the symbol name
as it appears in object code, (object files and libraries).  Confusion
may arise w.r.t. mingw64 and MSVC 64-bit, neither of which prepends the
underscore.  C++ may also introduce potential for confusion, since the
compiler will mangle any symbol name that is not declared to have extern
"C" linkage.

> Note that the above reference only has one
> underscore, instead of the two that show up
> when running "nm" on jvm.lib:
> 
> 00000000 I __imp__JNI_CreateJavaVM at 12
>         U __IMPORT_DESCRIPTOR_jvm
> 00000000 T _JNI_CreateJavaVM at 12

That looks correct; compare the corresponding references for Microsoft's
LoadLibraryA(), from our own libkernel32.a:

   $ nm mingw32/lib/libkernel32.a
   :
   00000000 I __imp__LoadLibraryA at 4
   00000000 T _LoadLibraryA at 4
   :
> I have tried a lot of searches and a lot of
> experiments, but nothing successful. I still
> can't even answer the basic question of
> whether it is meant to work out of the box
> or not.

I am unaware of any reason why it shouldn't work, and no ...

> Or whether I need to construct my own "lib" file.
... you already have an import library, which should work.

> Any ideas? Relevant technical info below.
Looks like the classical user error of getting the linking order utterly
wrong!  (It hasn't arisen recently, but used to come up regularly).

> C:\paul\devel\jnitest>mingw32-make
> mingw32-gcc -o mary.exe \
> -L "C:\Program Files (x86)\Java\jdk1.8.0_221\lib" \
> -ljvm \

This *is* wrong!  The linker will read libjvm.a, (having searched for
libjvm.dll.a, libjvm.a, and finally jvm.lib, in this order), *before* it
has even started building its table of required symbol references, find
that this library offers nothing which is required, (because *nothing*
is required yet), and dismiss it as irrelevant, without considering it
further.

> -I "C:\Program Files (x86)\Java\jdk1.8.0_221\include" \
> -I "C:\Program Files (x86)\Java\jdk1.8.0_221\include\win32" \
> mary.c

And only now, do you start building a table of required references,
*after* jvm.lib has been dismissed as contributing nothing.

> c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe:
> C:\Users\xxx\AppData\Local\Temp\ccqlQomh.o:mary.c:(.text+0x3c):
> undefined reference to `_imp__JNI_CreateJavaVM at 12'
> collect2.exe: error: ld returned 1 exit status
> Makefile:7: recipe for target 'mary.exe' failed
> mingw32-make: *** [mary.exe] Error 1

I would expect this, because the linking order is *wrong*; the -ljvm
reference *must* come *after* all .c or .o files which refer to it.

> C:\paul\devel\jnitest>mingw32-gcc --version
> mingw32-gcc (MinGW.org GCC-8.2.0-5) 8.2.0

You may wish to consider an upgrade ... we released a mingw32 GCC-9.2.0
in January 2020.

> mary.c:
> ret = JNI_CreateJavaVM(&jvm, (void**)&env, &args);

It would have been useful if you had shown us whatever declaration of
JNI_CreateJavaVM() is in scope, when this statement is compiled; we can
address your enquiry more effectively, when we are given *all* of the
pertinent information ... preferably as an SSCCE (see sscce.org).

-- 
Regards,
Keith.

Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://lists.osdn.me/mailman/archives/mingw-users/attachments/20200922/a394b120/attachment.sig>


More information about the MinGW-Users mailing list
Back to archive index