g++ gcc 在windows 10,64位环境下编译jni程序运行报错或者无法编译的解决方案
程序员文章站
2022-05-08 13:33:40
...
我的操作系统是windows10,安装的是 cygwin 64位,需要实现的功能很简单,java函数中调用jni函数打印 hello jni
HelloJni.java
public class HelloJni {
native void hello();
static {
System.loadLibrary("HelloJni");
}
public static void main(String args[]) {
HelloJni helloJni = new HelloJni();
helloJni.hello();
}
}
执行 javac HelloJni.java生成了HelloJni.class
执行javah HelloJni 生成HelloJni.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloJni */
#ifndef _Included_HelloJni
#define _Included_HelloJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJni
* Method: hello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloJni_hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
首次执行
g++ -I"C:\Program Files\Java\jdk1.8.0_171\include" -I"C:\Program Files\Java\jdk1.8.0_171\include\win
32" -shared HelloJni.c -o HelloJni.dll
报错
In file included from C:\Program Files\Java\jdk1.8.0_171\include/jni.h:45:0,
from HelloJni.h:2,
from HelloJni.c:2:
C:\Program Files\Java\jdk1.8.0_171\include\win32/jni_md.h:34:9:
error: '__int64' does not name a type
typedef __int64 jlong; ^~~~~~~In file included from HelloJni.h:2:0, from HelloJni.c:2:C:\Program Files\Java\jdk1.8.0_171\include/jni.h:126:5: error: 'jlong' does not name a type jlong j; ^~~~~C:\Program Files\Java\jdk1.8.0_171\include/jni.h:334:20: error: expected identifier before '*' token jlong (JNICALL *CallLongMethod) ^C:\Program Files\Java\jdk1.8.0_171\include/jni.h:334:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes] jlong (JNICALL *CallLongMethod) ^~~~~~~~~~~~~~C:\Program Files\Java\jdk1.8.0_171\include/jni.h:335:57: error: 'jlong' declared as function returning a function (JNIEnv *env, jobject obj, jmethodID methodID, ...); ^C:\Program Files\Java\jdk1.8.0_171\include/jni.h:336:20: error: expected identifier before '*' token jlong (JNICALL *CallLongMethodV) ^C:\Program Files\Java\jdk1.8.0_171\include/jni.h:336:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes] jlong (JNICALL *CallLongMethodV) ^~~~~~~~~~~~~~~C:\Program Files\Java\jdk1.8.0_171\include/jni.h:337:66: error: 'jlong' declared as function returning a function (JNIEnv *env, jobject obj, jmethodID methodID, va_list args); ^C:\Program Files\Java\jdk1.8.0_171\include/jni.h:338:20: error: expected identifier before '*' token jlong (JNICALL *CallLongMethodA)
提示很明显:
error: '__int64' does not name a type
加上 -D"__int64=long long" 重新编译 g++ -D"__int64=long long" -I"C:\Program Files\Java\jdk1.8.0_171\include" -I"C:\Program Files\Java\jd
k1.8.0_171\include\win32" -shared HelloJni.c -o HelloJni.dll
编译不报错执行java HelloJni
S C:\cygwin64\bin> java HelloJni
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180119727, pid=5624, tid=0x0000000000001a88
#
# JRE version: Java(TM) SE Runtime Environment (8.0_171-b11) (build 1.8.0_171-b11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [cygwin1.dll+0xd9727]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\cygwin64\bin\hs_err_pid5624.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
提示是一个Native执行错误 [cygwin1.dll+0xd9727]
打开生成的错误文件
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180119727, pid=5624, tid=0x0000000000001a88
#
# JRE version: Java(TM) SE Runtime Environment (8.0_171-b11) (build 1.8.0_171-b11)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [cygwin1.dll+0xd9727]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
--------------- T H R E A D ---------------
Current thread (0x0000000004f61000): JavaThread "main" [_thread_in_native, id=6792, stack(0x0000000004e60000,0x0000000004f60000)]
siginfo: ExceptionCode=0xc0000005, writing address 0x0000000000000000
Registers:
RAX=0x0000000000000000, RBX=0x0000000019610278, RCX=0x0000000522c43030, RDX=0x0000000004f5f7d0
RSP=0x0000000004f5f700, RBP=0x0000000004f5f730, RSI=0x0000000020000021, RDI=0x00000000ffffffff
R8 =0x0000000000000004, R9 =0x0000000004ac7a40, R10=0x0000000004f60000, R11=0x0000000522c410ac
R12=0x0000000000000000, R13=0x0000000019610278, R14=0x0000000004f5f7d0, R15=0x0000000004f61000
RIP=0x0000000180119727, EFLAGS=0x0000000000010202
Top of Stack: (sp=0x0000000004f5f700)
0x0000000004f5f700: 000000018018f7c0 000000018011973b
0x0000000004f5f710: 0000000004f5fa00 0000000004f61000
0x0000000004f5f720: 0000000019610278 0000000020000021
0x0000000004f5f730: 0000000004f5f7b0 0000000005078c67
0x0000000004f5f740: 0000000004f611f8 0000000004f5f7d0
0x0000000004f5f750: 0000000020000021 0000000004f61000
0x0000000004f5f760: 0000000004f5f830 0000000005078962
0x0000000004f5f770: 0000000004f5f770 0000000019610278
0x0000000004f5f780: 0000000004f5f7d0 0000000019610418
0x0000000004f5f790: 0000000000000000 0000000019610278
0x0000000004f5f7a0: 0000000000000000 0000000004f5f7d0
0x0000000004f5f7b0: 0000000004f5f818 000000000506835d
0x0000000004f5f7c0: 0000000000000000 00000000050721d8
0x0000000004f5f7d0: 00000000d5e5c6f8 0000000004f5f7d8
0x0000000004f5f7e0: 0000000019610319 0000000004f5f830
0x0000000004f5f7f0: 0000000019610418 0000000000000000
Instructions: (pc=0x0000000180119727)
0x0000000180119707: 04 f3 90 eb e3 48 c7 c0 08 00 00 00 49 0f c1 82
0x0000000180119717: 50 e4 ff ff 4c 8d 1d 19 00 00 00 4c 87 5c 24 08
0x0000000180119727: 4c 89 18 41 ff 82 44 e4 ff ff 41 ff 8a 4c e4 ff
0x0000000180119737: ff 58 ff e0 65 4c 8b 14 25 08 00 00 00 41 bb 01
Register to memory mapping:
RAX=0x0000000000000000 is an unknown value
RBX={method} {0x0000000019610280} 'hello' '()V' in 'HelloJni'
RCX=0x0000000522c43030 is an unknown value
RDX=0x0000000004f5f7d0 is pointing into the stack for thread: 0x0000000004f61000
RSP=0x0000000004f5f700 is pointing into the stack for thread: 0x0000000004f61000
RBP=0x0000000004f5f730 is pointing into the stack for thread: 0x0000000004f61000
RSI=0x0000000020000021 is an unknown value
RDI=0x00000000ffffffff is an unallocated location in the heap
R8 =0x0000000000000004 is an unknown value
R9 =0x0000000004ac7a40 is an unknown value
R10=0x0000000004f60000 is pointing into the stack for thread: 0x0000000004f61000
R11=0x0000000522c410ac is an unknown value
R12=0x0000000000000000 is an unknown value
R13={method} {0x0000000019610280} 'hello' '()V' in 'HelloJni'
R14=0x0000000004f5f7d0 is pointing into the stack for thread: 0x0000000004f61000
R15=0x0000000004f61000 is a thread
Stack: [0x0000000004e60000,0x0000000004f60000], sp=0x0000000004f5f700, free space=1021k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [cygwin1.dll+0xd9727]
C [cygwin1.dll+0x14f7bf]
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j HelloJni.hello()V+0
j HelloJni.main([Ljava/lang/String;)V+9
v ~StubRoutines::call_stub
--------------- P R O C E S S ---------------
Java Threads: ( => current thread )
0x000000001b0ee800 JavaThread "Service Thread" daemon [_thread_blocked, id=18524, stack(0x000000001b960000,0x000000001ba60000)]
0x0000000019bd3800 JavaThread "C1 CompilerThread2" daemon [_thread_blocked, id=9284, stack(0x000000001b860000,0x000000001b960000)]
0x0000000019bcc000 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=7312, stack(0x000000001b760000,0x000000001b860000)]
0x0000000019bc9000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=17148, stack(0x000000001b660000,0x000000001b760000)]
0x0000000019bc8000 JavaThread "Attach Listener" daemon [_thread_blocked, id=17460, stack(0x000000001b560000,0x000000001b660000)]
0x000000001b0a3800 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=12648, stack(0x000000001b460000,0x000000001b560000)]
0x0000000019b9a800 JavaThread "Finalizer" daemon [_thread_blocked, id=7152, stack(0x000000001ae00000,0x000000001af00000)]
0x0000000019b79800 JavaThread "Reference Handler" daemon [_thread_blocked, id=1492, stack(0x000000001ad00000,0x000000001ae00000)]
=>0x0000000004f61000 JavaThread "main" [_thread_in_native, id=6792, stack(0x0000000004e60000,0x0000000004f60000)]
Other Threads:
0x0000000019b77800 VMThread [stack: 0x000000001ac00000,0x000000001ad00000] [id=4888]
0x0000000019c0e800 WatcherThread [stack: 0x000000001ba60000,0x000000001bb60000] [id=6796]
VM state:not at safepoint (normal execution)
VM Mutex/Monitor currently owned by a thread: None
Heap:
PSYoungGen total 38400K, used 2004K [0x00000000d5d80000, 0x00000000d8800000, 0x0000000100000000)
eden space 33280K, 6% used [0x00000000d5d80000,0x00000000d5f75358,0x00000000d7e00000)
from space 5120K, 0% used [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000)
to space 5120K, 0% used [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000)
ParOldGen total 87552K, used 0K [0x0000000081800000, 0x0000000086d80000, 0x00000000d5d80000)
object space 87552K, 0% used [0x0000000081800000,0x0000000081800000,0x0000000086d80000)
Metaspace used 2555K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 281K, capacity 386K, committed 512K, reserved 1048576K
Card table byte_map: [0x0000000014420000,0x0000000014820000] byte_map_base: 0x0000000014014000
Marking Bits: (ParMarkBitMap*) 0x000000005aa108c0
Begin Bits: [0x0000000014ed0000, 0x0000000016e70000)
End Bits: [0x0000000016e70000, 0x0000000018e10000)
Polling page: 0x0000000004a80000
CodeCache: size=245760Kb used=1106Kb max_used=1106Kb free=244654Kb
bounds [0x0000000005060000, 0x00000000052d0000, 0x0000000014060000]
total_blobs=258 nmethods=24 adapters=148
compilation: enabled
Compilation events (10 events):
Event: 0.107 Thread 0x0000000019bd3800 20 s 3 java.lang.StringBuffer::append (13 bytes)
Event: 0.108 Thread 0x0000000019bd3800 nmethod 20 0x0000000005171fd0 code [0x0000000005172180, 0x0000000005172898]
Event: 0.113 Thread 0x0000000019bd3800 21 3 java.lang.AbstractStringBuilder::append (50 bytes)
Event: 0.114 Thread 0x0000000019bd3800 nmethod 21 0x0000000005173450 code [0x0000000005173620, 0x0000000005173d78]
Event: 0.115 Thread 0x0000000019bd3800 22 3 java.lang.Math::min (11 bytes)
Event: 0.115 Thread 0x0000000019bd3800 nmethod 22 0x0000000005174090 code [0x00000000051741e0, 0x0000000005174370]
Event: 0.116 Thread 0x0000000019bd3800 23 3 sun.nio.cs.ext.DoubleByte$Encoder::encodeChar (21 bytes)
Event: 0.116 Thread 0x0000000019bd3800 nmethod 23 0x00000000051743d0 code [0x0000000005174540, 0x00000000051746f0]
Event: 0.116 Thread 0x0000000019bd3800 24 3 java.lang.System::getSecurityManager (4 bytes)
Event: 0.116 Thread 0x0000000019bd3800 nmethod 24 0x0000000005174850 code [0x00000000051749a0, 0x0000000005174ad0]
GC Heap History (0 events):
No events
Deoptimization events (0 events):
No events
Classes redefined (0 events):
No events
Internal exceptions (2 events):
Event: 0.039 Thread 0x0000000004f61000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x00000000d5d87cb0) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u171\10807\hotspot
Event: 0.039 Thread 0x0000000004f61000 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x00000000d5d87f98) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u171\10807\hotspot\src\share\vm\prim
Events (10 events):
Event: 0.113 loading class java/security/BasicPermissionCollection
Event: 0.114 loading class java/security/BasicPermissionCollection done
Event: 0.114 loading class sun/launcher/LauncherHelper$FXHelper
Event: 0.114 loading class sun/launcher/LauncherHelper$FXHelper done
Event: 0.114 loading class java/lang/Class$MethodArray
Event: 0.114 loading class java/lang/Class$MethodArray done
Event: 0.114 loading class java/lang/Void
Event: 0.114 loading class java/lang/Void done
Event: 0.115 loading class java/lang/ClassLoaderHelper
Event: 0.115 loading class java/lang/ClassLoaderHelper done
Dynamic libraries:
0x00007ff68a2b0000 - 0x00007ff68a2e7000 C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
0x00007ff9104a0000 - 0x00007ff910680000 C:\WINDOWS\SYSTEM32\ntdll.dll
0x00007ff910310000 - 0x00007ff9103be000 C:\WINDOWS\System32\KERNEL32.DLL
0x00007ff90ca50000 - 0x00007ff90ccb6000 C:\WINDOWS\System32\KERNELBASE.dll
0x00007ff90dfb0000 - 0x00007ff90e051000 C:\WINDOWS\System32\ADVAPI32.dll
0x00007ff90f810000 - 0x00007ff90f8ad000 C:\WINDOWS\System32\msvcrt.dll
0x00007ff90f8e0000 - 0x00007ff90f93b000 C:\WINDOWS\System32\sechost.dll
0x00007ff90e060000 - 0x00007ff90e17f000 C:\WINDOWS\System32\RPCRT4.dll
0x00007ff90d970000 - 0x00007ff90daff000 C:\WINDOWS\System32\USER32.dll
0x00007ff90d130000 - 0x00007ff90d150000 C:\WINDOWS\System32\win32u.dll
0x00007ff90deb0000 - 0x00007ff90ded8000 C:\WINDOWS\System32\GDI32.dll
0x00007ff90ccc0000 - 0x00007ff90ce53000 C:\WINDOWS\System32\gdi32full.dll
0x00007ff90d090000 - 0x00007ff90d12b000 C:\WINDOWS\System32\msvcp_win.dll
0x00007ff90c8a0000 - 0x00007ff90c996000 C:\WINDOWS\System32\ucrtbase.dll
0x00007ff8ff710000 - 0x00007ff8ff979000 C:\WINDOWS\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.16299.431_none_15c7d3ee93659e73\COMCTL32.dll
0x00007ff90dba0000 - 0x00007ff90dea8000 C:\WINDOWS\System32\combase.dll
0x00007ff90d1a0000 - 0x00007ff90d212000 C:\WINDOWS\System32\bcryptPrimitives.dll
0x00007ff90f8b0000 - 0x00007ff90f8dd000 C:\WINDOWS\System32\IMM32.DLL
0x000000005aa90000 - 0x000000005ab62000 C:\Program Files\Java\jre1.8.0_171\bin\msvcr100.dll
0x000000005a1f0000 - 0x000000005aa90000 C:\Program Files\Java\jre1.8.0_171\bin\server\jvm.dll
0x00007ff910280000 - 0x00007ff910288000 C:\WINDOWS\System32\PSAPI.DLL
0x00007ff8feab0000 - 0x00007ff8feab9000 C:\WINDOWS\SYSTEM32\WSOCK32.dll
0x00007ff90a270000 - 0x00007ff90a293000 C:\WINDOWS\SYSTEM32\WINMM.dll
0x00007ff90af90000 - 0x00007ff90af9a000 C:\WINDOWS\SYSTEM32\VERSION.dll
0x00007ff90e180000 - 0x00007ff90e1ec000 C:\WINDOWS\System32\WS2_32.dll
0x00007ff90a210000 - 0x00007ff90a23a000 C:\WINDOWS\SYSTEM32\winmmbase.dll
0x00007ff90d150000 - 0x00007ff90d19a000 C:\WINDOWS\System32\cfgmgr32.dll
0x000000005b180000 - 0x000000005b18f000 C:\Program Files\Java\jre1.8.0_171\bin\verify.dll
0x000000005ae40000 - 0x000000005ae69000 C:\Program Files\Java\jre1.8.0_171\bin\java.dll
0x000000005b160000 - 0x000000005b176000 C:\Program Files\Java\jre1.8.0_171\bin\zip.dll
0x00007ff90e3d0000 - 0x00007ff90f808000 C:\WINDOWS\System32\SHELL32.dll
0x00007ff9103c0000 - 0x00007ff910466000 C:\WINDOWS\System32\shcore.dll
0x00007ff90d220000 - 0x00007ff90d967000 C:\WINDOWS\System32\windows.storage.dll
0x00007ff90e1f0000 - 0x00007ff90e241000 C:\WINDOWS\System32\shlwapi.dll
0x00007ff90c880000 - 0x00007ff90c891000 C:\WINDOWS\System32\kernel.appcore.dll
0x00007ff90c810000 - 0x00007ff90c85c000 C:\WINDOWS\System32\powrprof.dll
0x00007ff90c860000 - 0x00007ff90c87b000 C:\WINDOWS\System32\profapi.dll
0x0000000522c40000 - 0x0000000522c6b000 C:\cygwin64\bin\HelloJni.dll
0x0000000180040000 - 0x0000000180600000 C:\cygwin64\bin\cygwin1.dll
0x00007ff90c200000 - 0x00007ff90c20b000 C:\WINDOWS\SYSTEM32\CRYPTBASE.DLL
0x00007ff90b2a0000 - 0x00007ff90b468000 C:\WINDOWS\SYSTEM32\dbghelp.dll
VM Arguments:
java_command: HelloJni
java_class_path (initial): .
Launcher Type: SUN_STANDARD
Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_171
PATH=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files (x86)\Git\cmd;C:\cygwin64\bin;C:\Users\liupengcheng\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk-10.0.1\bin;
USERNAME=liupengcheng
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
--------------- S Y S T E M ---------------
OS: Windows 10.0 , 64 bit Build 16299 (10.0.16299.431)
CPU:total 4 (initial active 4) (2 cores per cpu, 2 threads per core) family 6 model 142 stepping 9, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, avx2, aes, clmul, erms, 3dnowpref, lzcnt, ht, tsc, tscinvbit, bmi1, bmi2, adx
Memory: 4k page, physical 8284900k(2167564k free), swap 15624932k(3294976k free)
vm_info: Java HotSpot(TM) 64-Bit Server VM (25.171-b11) for windows-amd64 JRE (1.8.0_171-b11), built on Mar 28 2018 16:06:12 by "java_re" with MS VC++ 10.0 (VS2010)
time: Mon May 14 22:08:59 2018
elapsed time: 1 seconds (0d 0h 0m 1s)
根据提示内容。找到肯定是我们编译的文件不符合要求
在cygwin64/bin目录下,有好几种编译器 ,我们选择
x86_64-w64-mingw32-g++ -I"C:\Program Files\Java\jdk1.8.0_171\include" -I"C:\Program Files\Java\jdk1
.8.0_171\include\win32" -shared HelloJni.c -o HelloJni.dll
java HelloJni
hello jni
终于可以输出Hello jni 了,原因分析:
g++ 和
x86_64-w64-mingw32-g++ g++比较适用于linux上面编译运行,而windows中,由于变量定义的差异性,当我们强制做是的时候程序就会报错
上一篇: maven
下一篇: Hello World 项目起步