g++ gcc 在windows 10,64位环境下编译jni程序运行报错或者无法编译的解决方案

我的操作系统是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 
/* 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

你可能感兴趣的:(JNI,g++,JNI,windows10)