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

在Windows 10 64位系统中,使用cygwin 64位环境,遇到g++/gcc编译JNI程序时出现错误。通过分析错误提示,发现是编译器选择问题。通过选择正确的编译器版本,成功编译并运行程序,输出'Hello jni',解决了问题。
摘要由CSDN通过智能技术生成

我的操作系统是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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值