Menu

Using Native C Functions

Peter
2016-01-24
2016-01-25
  • Peter

    Peter - 2016-01-24

    I have some problems with native functions in HaikuVM.

    I have the following simple code example, where i want to make the blink part in C instead of in Java.

    package main;
    
    public class Blink {  
    
        public static native void Main();  
    
        public static native void nBlink();  
    
        public static void main(String[] args) {  
            // Data direction of I/O-Port. 
            Main();
            while (true) {  
                nBlink();
            }  
        }  
    }  
    

    The functions gets generated in haikuJNI.c as mentioned in the tutorial, e.g.

    /*
     * Class:     Blink
     * Method:    Main
     * Signature: ()V
     */
    JNIEXPORT void Java_Blink_Main
      (JNIEnv *, jclass);
    
    JNIEXPORT void Java_Blink_Main JNICALL
      (JNIEnv *env, jclass obj)
    {
        // TODO: insert your code here
    }
    
    /*
     * Proprietary HaikuVM stack to JNI Adapter function.
     * DO NOT EDIT THIS FUNCTION - it is machine generated.
     * 
     * Class:     Blink
     * Method:    Main
     * Signature: ()V
     */
    JNIEXPORT void native_Blink_Main_V(void) {
        pushTop();    // Save variable top onto stack.
        {
            jclass     obj = NULL;
            JNIEnv     *env = (JNIEnv*)⊤
            Java_Blink_Main((JNIEnv*)&env, obj);
        }
        popTop();
    }
    

    But the block with the "TODO" comment always gets overwritten when compling. What am I doing wrong? I Also tried the @NativeCFunction flag, but it did not generate any functions in nativeCFunctions.cpp

    What am I doing wrong, and also have can i use includes in these native functions?

    Thanks in advance.

    I'm also getting the following error:

    ./HaikuVM/utility/nativeCFunctions.o: In function `native_Blink_nBlink_V':
    nativeCFunctions.cpp:(.text.native_Blink_nBlink_V+0x4): undefined reference to `nBlink'
    
     

    Last edit: Peter 2016-01-24
    • genom2

      genom2 - 2016-01-25

      Hello Peter,

      thank you for using HaikuVM.

      What you see in 'haikuJNI.c' is a HaikuVM generated blueprint of the JNI wrapper. It's made to copy&past it into some C file of your project directory as stated in this tutorial: http://haiku-vm.sourceforge.net/#[[JNI%20simple]]
      (The file 'haikuJNI.c' is not made for compilation with a C compiler. It's just made for you, as a copy&past source and therefore is overwritten/generated on any run of HaikuVM.)

      In your case try this.
      Make some directory 'firstJNIProject':

      mkdir firstJNIProject
      mkdir firstJNIProject\myFirstJNIfiles
      cd firstJNIProject
      

      Haikufy your Java program:

      C:\HaikuVM\bin\haiku --Config duemilanove --classpath C:\myWorkspace\src\main\java C:\myWorkspace\src\main\java\main\Blink.java 
      

      And yes, in this state you get some 'undefined reference' error like:

      ./HaikuVM/JVM.o:(.progmem.data+0x2d8): undefined reference to `native_main_Blink_Main_V'
      ./HaikuVM/JVM.o:(.progmem.data+0x2db): undefined reference to `native_main_Blink_nBlink_V'
      

      But now it's happy copy&past time. Copy the part you mentioned from 'haikuJNI.c' into (let's say) file 'firstJNIProject\myFirstJNIfiles\myNativeCFunctions.cpp'. After some additional editing, your C++ file 'myNativeCFunctions.cpp' should look like this:

      #include <jni.h>
      
      #ifdef __cplusplus
      extern "C" {
      #endif
      
      /*
       * Class:     main.Blink
       * Method:    Main
       * Signature: ()V
       */
      JNIEXPORT void Java_main_Blink_Main
        (JNIEnv *, jclass);
      
      JNIEXPORT void Java_main_Blink_Main JNICALL
        (JNIEnv *env, jclass obj)
      {
          // TODO: insert your code here
      }
      
      /*
       * Proprietary HaikuVM stack to JNI Adapter function.
       * DO NOT EDIT THIS FUNCTION - it is machine generated.
       * 
       * Class:     main.Blink
       * Method:    Main
       * Signature: ()V
       */
      JNIEXPORT void native_main_Blink_Main_V(void) {
          pushTop();    // Save variable top onto stack.
          {
              jclass     obj = NULL;
              JNIEnv     *env = (JNIEnv*)&top;
              Java_main_Blink_Main((JNIEnv*)&env, obj);
          }
          popTop();
      }
      
      /////////////////////////////////////////////////
      
      
      /*
       * Class:     main.Blink
       * Method:    nBlink
       * Signature: ()V
       */
      JNIEXPORT void Java_main_Blink_nBlink
        (JNIEnv *, jclass);
      
      JNIEXPORT void Java_main_Blink_nBlink JNICALL
        (JNIEnv *env, jclass obj)
      {
          // TODO: insert your code here
      }
      
      /*
       * Proprietary HaikuVM stack to JNI Adapter function.
       * DO NOT EDIT THIS FUNCTION - it is machine generated.
       * 
       * Class:     main.Blink
       * Method:    nBlink
       * Signature: ()V
       */
      JNIEXPORT void native_main_Blink_nBlink_V(void) {
          pushTop();    // Save variable top onto stack.
          {
              jclass     obj = NULL;
              JNIEnv     *env = (JNIEnv*)&top;
              Java_main_Blink_nBlink((JNIEnv*)&env, obj);
          }
          popTop();
      }
      
      #ifdef __cplusplus
      }
      #endif
      

      Give it a try:

      C:\HaikuVM\bin\haiku --Config duemilanove --classpath C:\myWorkspace\src\main\java C:\myWorkspace\src\main\java\main\Blink.java 
      

      Now all 'undefined reference' errors should be resolved. You are ready to fill the "TODO" comments with your code.

      Is this of some help for you?

       

      Last edit: genom2 2016-01-25

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.