netbean 和 cygwin 的 C++ 编译错误?

标签 c++ java-native-interface cygwin

我的程序代码

#include "HelloWorld.h"
#include <stdio.h>
#include <windows.h>
#include <signal.h>
using namespace std;
JNIEnv *cur_env; /* pointer to native method interface */
void registerWndProc();
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv *env, jobject){
cur_env = env; /* Creates window & enters message loop */
registerWndProc();
}
void calljvm() {
JavaVM *jvm;
cur_env->GetJavaVM(&jvm);
jint AttachCurrentThread = (*jvm).AttachCurrentThread((void **)(&cur_env),NULL);
jclass cls=cur_env->FindClass("helloworld/HelloWorld");
jmethodID mid=cur_env->GetStaticMethodID(cls,"callback","()V");      
(*cur_env).CallStaticVoidMethod(cls, mid);
(*jvm).DetachCurrentThread();
} 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam)
{
switch(msg) {
    case WM_ENDSESSION :
        calljvm();
    break;
    default:
    break;
}

return DefWindowProc(hwnd, msg, wParam, lParam);
}
void registerWndProc() {
HINSTANCE h2 = GetModuleHandle(NULL); 
static char szAppName[] = "winhello";
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = h2;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL; 
/* Register a new window class with Windows */
if (!RegisterClassEx(&wndclass)) {
    MessageBox(NULL, TEXT("Registering class failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
    return;
} 
/* Create a window based on our new class */
HWND hwnd; hwnd = CreateWindow(szAppName, "Hello, world!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, h2, NULL); 
/* CreateWindow failed? */
if( !hwnd ) {
    MessageBox(NULL, TEXT("Window creation failed!"), TEXT("Error!"), MB_ICONEXCLAMATION | MB_OK);
    return;
} 
/* Show and update our window */
ShowWindow(hwnd, 0); /* 0 = SW_HIDE */
UpdateWindow(hwnd); MSG msg;
while (GetMessage(&msg, NULL, 0, 0) ) {
    TranslateMessage(&msg); /* for certain keyboard messages */
    DispatchMessage(&msg); /* send message to WndProc */
 }
} 

当我在netbean中编译时编译失败

cd 'C:\Users\ma\Documents\NetBeansProjects\HelloWorld'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/ma/Documents/NetBeansProjects/HelloWorld'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_1-Windows/libHelloWorld.dll
make[2]: Entering directory '/cygdrive/c/Users/ma/Documents/NetBeansProjects/HelloWorld'
mkdir -p build/Debug/Cygwin_1-Windows
rm -f "build/Debug/Cygwin_1-Windows/helloworld.o.d"
g++    -c -g -I../../../../../Program\ Files/Java/jdk1.8.0_144/include -I../../HelloWorld/src -include ../../HelloWorld/src/HelloWorld.h  -MMD -MP -MF "build/Debug/Cygwin_1-Windows/helloworld.o.d" -o build/Debug/Cygwin_1-Windows/helloworld.o helloworld.cpp
In file included from /cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:45:0,
                 from ./../../HelloWorld/src/HelloWorld.h:2,
                 from <command-line>:31:
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni_md.h:34:9: error: '__int64' does not name a type; did you mean '__int64_t'?
 typedef __int64 jlong;
         ^~~~~~~
         __int64_t
In file included from ./../../HelloWorld/src/HelloWorld.h:2:0,
                 from <command-line>:31:
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:126:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong    j;
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:334:20: error: expected identifier before '*' token
     jlong (JNICALL *CallLongMethod)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:334:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallLongMethod)
                     ^~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:335:57: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jobject obj, jmethodID methodID, ...);
                                                         ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:336:20: error: expected identifier before '*' token
     jlong (JNICALL *CallLongMethodV)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:336:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallLongMethodV)
                     ^~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:337:66: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jobject obj, jmethodID methodID, va_list args);
                                                                  ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:338:20: error: expected identifier before '*' token
     jlong (JNICALL *CallLongMethodA)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:338:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallLongMethodA)
                     ^~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:339:72: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args);
                                                                        ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:416:20: error: expected identifier before '*' token
     jlong (JNICALL *CallNonvirtualLongMethod)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:416:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallNonvirtualLongMethod)
                     ^~~~~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:417:71: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jobject obj, jclass clazz, jmethodID methodID, ...);
                                                                       ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:418:20: error: expected identifier before '*' token
     jlong (JNICALL *CallNonvirtualLongMethodV)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:418:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallNonvirtualLongMethodV)
                     ^~~~~~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:420:20: error: 'jlong' declared as function returning a function
        va_list args);
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:421:20: error: expected identifier before '*' token
     jlong (JNICALL *CallNonvirtualLongMethodA)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:421:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallNonvirtualLongMethodA)
                     ^~~~~~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:423:26: error: 'jlong' declared as function returning a function
        const jvalue *args);
                          ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:467:20: error: expected identifier before '*' token
     jlong (JNICALL *GetLongField)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:467:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *GetLongField)
                     ^~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:468:50: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jobject obj, jfieldID fieldID);
                                                  ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:487:52: error: 'jlong' has not been declared
       (JNIEnv *env, jobject obj, jfieldID fieldID, jlong val);
                                                    ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:538:20: error: expected identifier before '*' token
     jlong (JNICALL *CallStaticLongMethod)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:538:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallStaticLongMethod)
                     ^~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:539:58: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jclass clazz, jmethodID methodID, ...);
                                                          ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:540:20: error: expected identifier before '*' token
     jlong (JNICALL *CallStaticLongMethodV)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:540:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallStaticLongMethodV)
                     ^~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:541:67: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);
                                                                   ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:542:20: error: expected identifier before '*' token
     jlong (JNICALL *CallStaticLongMethodA)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:542:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *CallStaticLongMethodA)
                     ^~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:543:73: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args);
                                                                         ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:580:20: error: expected identifier before '*' token
     jlong (JNICALL *GetStaticLongField)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:580:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *GetStaticLongField)
                     ^~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:581:51: error: 'jlong' declared as function returning a function
       (JNIEnv *env, jclass clazz, jfieldID fieldID);
                                                   ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:600:53: error: 'jlong' has not been declared
       (JNIEnv *env, jclass clazz, jfieldID fieldID, jlong value);
                                                     ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:662:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong * (JNICALL *GetLongArrayElements)
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:680:39: error: 'jlong' has not been declared
       (JNIEnv *env, jlongArray array, jlong *elems, jint mode);
                                       ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:697:63: error: 'jlong' has not been declared
       (JNIEnv *env, jlongArray array, jsize start, jsize len, jlong *buf);
                                                               ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:714:69: error: 'jlong' does not name a type; did you mean 'ulong'?
       (JNIEnv *env, jlongArray array, jsize start, jsize len, const jlong *buf);
                                                                     ^~~~~
                                                                     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:758:37: error: 'jlong' has not been declared
        (JNIEnv* env, void* address, jlong capacity);
                                     ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:761:20: error: expected identifier before '*' token
     jlong (JNICALL *GetDirectBufferCapacity)
                    ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:761:21: warning: '__stdcall__' attribute only applies to function types [-Wattributes]
     jlong (JNICALL *GetDirectBufferCapacity)
                     ^~~~~~~~~~~~~~~~~~~~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:762:33: error: 'jlong' declared as function returning a function
        (JNIEnv* env, jobject buf);
                                 ^
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1000:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallLongMethod(jobject obj, jmethodID methodID, ...) {
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1008:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallLongMethodV(jobject obj, jmethodID methodID,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1012:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallLongMethodA(jobject obj, jmethodID methodID,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1192:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallNonvirtualLongMethod(jobject obj, jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1202:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallNonvirtualLongMethodV(jobject obj, jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1207:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallNonvirtualLongMethodA(jobject obj, jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1300:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong GetLongField(jobject obj, jfieldID fieldID) {
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1334:23: error: 'jlong' has not been declared
                       jlong val) {
                       ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1459:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallStaticLongMethod(jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1468:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallStaticLongMethodV(jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1472:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong CallStaticLongMethodA(jclass clazz,
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1550:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong GetStaticLongField(jclass clazz, jfieldID fieldID) {
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1585:25: error: 'jlong' has not been declared
                         jlong value) {
                         ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1679:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong * GetLongArrayElements(jlongArray array, jboolean *isCopy) {
     ^~~~~
     ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1715:35: error: 'jlong' has not been declared
                                   jlong *elems,
                                   ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1751:53: error: 'jlong' has not been declared
                             jsize start, jsize len, jlong *buf) {
                                                     ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1784:35: error: 'jlong' does not name a type; did you mean 'ulong'?
                             const jlong *buf) {
                                   ^~~~~
                                   ulong
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1847:48: error: 'jlong' has not been declared
     jobject NewDirectByteBuffer(void* address, jlong capacity) {
                                                ^~~~~
/cygdrive/c/Program Files/Java/jdk1.8.0_144/include/jni.h:1853:5: error: 'jlong' does not name a type; did you mean 'ulong'?
     jlong GetDirectBufferCapacity(jobject buf) {
     ^~~~~
     ulong
make[2]: *** [nbproject/Makefile-Debug.mk:68: build/Debug/Cygwin_1-Windows/helloworld.o] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/ma/Documents/NetBeansProjects/HelloWorld'
make[1]: *** [nbproject/Makefile-Debug.mk:59: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/ma/Documents/NetBeansProjects/HelloWorld'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 2s)

最佳答案

我找到了我的问题的解决方案。因为 GCC 没有内置的 __int64,这个补丁基本上使用“long long”而不是我们必须编辑文件/include/win32/jni_md.h,安装根目录在哪里(例如,c:/jdk1 .4.2). 替换代码

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

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

有用的资源

https://gist.github.com/ssfang/e52c00cd446081cd72a982a2dd8634d4

Error when compiling in cygwin

关于netbean 和 cygwin 的 C++ 编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51755226/

相关文章:

java - Hadoop 中奇怪的 UnsatisfiedLinkError

terminal - ConEmu/cygwin 换行符不执行回车功能

java - 从 const 方法调用 JNI 函数

java - 如何在 OS X 上正确创建 C++ 对象文件 (.o)?

c - 是否有用于使用 cygwin 为 nppexec 编译 C 代码的脚本?

search - 递归使用grep

c++ - 要求编辑文本,文本格式

c++ - 水平面方程

c++ - 对 Windows HANDLE 使用 std::unique_ptr

c++ - execl(linux)中的路径