0

Am just starting learn jni and decided to do a hello world. i created a helloworld java file whose code is

public class HelloWorld {
    public native void displayHelloWorld();
    static{
        System.loadLibrary("hello");
    }

    public static void main(String[] args){
        new HelloWorld().displayHelloWorld();
    }
    
}

and i compiled it to get the following header file

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    displayHelloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

followed by creating a c file whose code is

#include<jni.h>
#include "HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj){
    printf("Hello World \n");
    return;
}

and compiled it to create the helloworld.o file

and now when i tried to create the .dll file using the command

gcc -shared -o hello.dll HelloWorld.o -Wl,--add-stdcall-alias

i get this error

At line:1 char:42
+ gcc -shared -o hello.dll HelloWorld.o -Wl,--add-stdcall-alias
+                                          ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

what am i missing?

Botje
  • 26,269
  • 3
  • 31
  • 41
  • 1
    See https://stackoverflow.com/questions/52204105/tdm-gccs-mingw32-gcc-throws-missing-argument-error – Michael Jul 30 '22 at 13:11

1 Answers1

2

The problem is that in PowerShell comma has a special meaning of comma operator unlike, e.g., in Bash, where in such context there is no any special meaning to it. When your command is parsed, -Wl is considered a parameter name and , is interpreted to form an array (parameter list) as its argument. As seen in linked documentation, , is necessarily binary operator unless in expression mode, whereas for command invocation argument mode is used. But there's no left argument for this comma, thus Missing argument in parameter list error.

To solve this, you can change the command to make PowerShell stop intepreting , in a special way by making -Wl,--add-stdcall-alias a quoted string using one of the types of strings. For our simple case any string type would do, e.g. with single-quoted strings we have:

gcc -shared -o hello.dll HelloWorld.o '-Wl,--add-stdcall-alias'

Another option is to use stop parsing token --%:

gcc --% -shared -o hello.dll HelloWorld.o -Wl,--add-stdcall-alias
YurkoFlisk
  • 873
  • 9
  • 21