Home > Net >  Need help compiling a JNI program on VS2022
Need help compiling a JNI program on VS2022

Time:04-18

Am trying to compile a very elementary JNI program. The Java code is :

public class Helloworld {
    public static void main(String[] args) {
        System.out.println("Hello World !!");
        new Helloworld().sayHello();
    }

    private native void sayHello();
}

The next step I took was to compile it outside the IDE (Intellij IDEA), using the command line option of javac -h ... This produces a machine generated Helloworld.h file. Next I wrote an equally elementary C program, code here :

#include "Helloworld.h"

JNIEXPORT void JNICALL Java_Helloworld_sayHello(JNIEnv *, jobject){
  printf("\nSay something... Anything...");
 }

This I tried compiling using VS 2022 dll option, first with pre-compiled headers, and then without the precompiled headers, but no luck. A vast array of errors manifested themselves. It would have been ideal to have this compiled within the VS environment, since the actual code that is planned to be called from Java contains a mix of C and AVX512 assembly, so having the IDE support would have been ideal (especially during debugging). Any advice on how to compile this to a .dll library that would be acceptable to Java would be very welcome.

Then I moved out of VS 2022, and used the following command line compilation string :

cl /c /I "C:\\Program Files\\Microsoft\\jdk\\include\\" /I "C:\\Program Files\\Microsoft\\jdk\\include\\win32\\" .\Helloworld.c

This gives the following very persistent error :

Microsoft (R) C/C Optimizing Compiler Version 19.31.31106.2 for x64 Copyright (C) Microsoft Corporation. All rights reserved.

Helloworld.c

.\Helloworld.c(3): error C2055: expected formal parameter list, not a type list

I tried changing the formal parameter from jobject to jobject j, but that did not succeed (obviously, since the definition in the jni.h states jobject is a typedef for a _jobject *). I tried to undefine the __cplusplus by using the cl command option /U __cplusplus but no joy. Not sure what to try next.

Any advice is welcome. Thanks

CodePudding user response:

From Microsoft's documentation:

expected formal parameter list, not a type list

A function definition contains a parameter type list instead of a formal parameter list. ANSI C requires formal parameters to be named unless they are void or an ellipsis (...).

void func(int, char) {}        // C2055
void func (int i, char c) {}   // OK

Your function definition is the one in your .c file, i.e.

JNIEXPORT void JNICALL Java_Helloworld_sayHello(JNIEnv *, jobject) {
    ...
}

which you need to change into something like

JNIEXPORT void JNICALL Java_Helloworld_sayHello(JNIEnv *env, jobject thiz) {
    ...
}

Any corresponding function declaration (e.g. in a .h file) should work with or without parameter names.

  • Related