library create and application - hqzhang/cloudtestbed GitHub Wiki

Static Loading libary

gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o
gcc -L/home/hongqi/sharelib -Wall -o test main.c -lfoo -lpthread
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/hongqi/sharelib
./test
//foo.c
#include <stdio.h>
void foo(void)   
    puts("Hello, I'm a shared library");
}
//foo.h
#ifndef foo_h__
#define foo_h__
extern void foo(void);
#endif
//main.c
#include “foo.h”
int main(){
    foo();
}

Dynamic Loading library

gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o
#gcc -L/home/hongqi/sharelib -Wall -o test main.c -lfoo
gcc -o test main.c -ldl
./test
//foo.c
//foo.h
//main.c 
        #include <stdlib.h>
    #include <stdio.h>
    #include <dlfcn.h>

    int main(int argc, char **argv) {
        void *handle;
        void (*foofunc)();
        char *error;
        printf("open the shared library\n");
        handle = dlopen ("/home/hongqi/shareld/libfoo.so", RTLD_LAZY);
        if (!handle) {
            fputs (dlerror(), stderr);
            exit(1);
        }
        printf("get symbol of foo function\n");
        foofunc = dlsym(handle, "foo");
        if ((error = dlerror()) != NULL)  {
            fputs(error, stderr);
            exit(1);
        }
        printf("run the foo function\n");
         (*foofunc)();
        dlclose(handle);
}

Dynamic Loading library with interface

gcc -c -Wall -Werror -fpic foo.c
gcc -shared -o libfoo.so foo.o
#gcc -L/home/hongqi/sharelib -Wall -o test main.c -lfoo
gcc -o test main.c -ldl
./test
//foo.c
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include "ulp.h"
//extern "C" {
int ulp_init(UlpCallbacks *cb);
void forward ();
static ulpInterface ulpInf =
{
   sizeof(ulpInterface),
   ulp_init,
   forward
};

UlpCallbacks *globalcb;

ulpInterface* ulp_get_interface () //call this by upper cpp
{
    printf("%s","enter ulp_get_interface ().\n");
   return &ulpInf;
}

int ulp_init(UlpCallbacks *cb){   //init by upper cpp

    printf("%s","enter ulp_init function to set callback\n");
    globalcb = cb;
    return 0;
}
void forward () {//forward to callback to upper cpp
    printf("%s","enter  forward () and call callback from main\n");
    globalcb->location_cb();
}             

//ulp.h
#include <stdio.h>
//#ifdef __cplusplus
//extern "C" {
//#endif
typedef void (* ulp_callback)(void);

/** ULP callback structure. */
typedef struct {
    size_t      size;
    ulp_callback location_cb;
} UlpCallbacks;

typedef struct {
    unsigned int   size;
    int   (*init)(UlpCallbacks *cb);
    void  (*forw)(void);
}ulpInterface;
typedef const ulpInterface* (get_ulp_interface) (void);
//main.c
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include "ulp.h"
const ulpInterface* loc_eng_ulpInf = NULL;
static void location_callback(){
    printf("%s","enter  locationcallback in main\n");

}
UlpCallbacks sUlpCallbacks = {
    sizeof(UlpCallbacks),
    location_callback
};

int main(int argc, char **argv) {
        void *module;
        void (*foofunc)();
        char *error;
        get_ulp_interface* get_ulp_inf;

        printf("open the shared library\n");
        module = dlopen ("/home/hongqi/sharefinal/libfoo.so", RTLD_LAZY);
        if (!module) {
            fputs (dlerror(), stderr);
            exit(1);
        }
        printf("get symbol of interface function\n");
        get_ulp_inf = (get_ulp_interface*) dlsym(module , "ulp_get_interface");
        if ((error = dlerror()) != NULL)  {
            fputs(error, stderr);
            exit(1);
        }
        printf("run the interface function\n");
        loc_eng_ulpInf = get_ulp_inf(); //for interface design
        printf("run the init function in module for Callback\n");
        loc_eng_ulpInf->init(&sUlpCallbacks);
        printf("run the real function in module\n");
        loc_eng_ulpInf->forw();
        dlclose(module);
}

Dynamic Loading library with JNI

g++ -c -Wall  -fpic hellojni.cpp -I/usr/lib/jvm/java-7-openjdk-i386/include
g++ -shared -o libhellojni.so hellojni.o
#gcc -L/home/hongqi/sharelib -Wall -o test main.c -lfoo
javac HelloJNI.java
java -Djava.library.path=. HelloJNI
//hellojni.cpp
#include <string.h>
#include <stdio.h>
#include <jni.h>
#include "ulp.h"
#include <dlfcn.h>
#include <stdlib.h>

static jobject jWiper = NULL;
static jmethodID midCallBackStr;
int call_function();

//create object wiper
static void instanceInit(JNIEnv *env, jobject obj) {
     printf("enter instanceInit \n");
     if (NULL == jWiper) {
         jWiper = env->NewGlobalRef(obj);
     }
}
static void classInit(JNIEnv* env, jclass clazz) {
         printf("enter classInit()  \n");
         midCallBackStr = (env)->GetMethodID( clazz,"callback", "(Ljava/lang/String;)V");
 }

 static jstring stringFromJNI (JNIEnv *env, jobject Obj) //called by jv
 {
        //jclass thisClass = (env)->GetObjectClass( Obj);
        //jmethodID midCallBackStr = (env)->GetMethodID( thisClass,
        //                           "callback", "(Ljava/lang/String;)V");
        //if (NULL == midCallBackStr) return;

        printf("enter stringFromjni3333333 In C++, call back Java's called(String)\n");
        jstring message = (env)->NewStringUTF( "Hello from C++");
        (env)->CallVoidMethod( Obj, midCallBackStr, message);

        return env->NewStringUTF("Hello from C++ over JNI!");
}
 static jstring callback(JNIEnv *env, jobject Obj){       //callback
         jstring message = (env)->NewStringUTF( "Hello from C++");
                (env)->CallVoidMethod( Obj, midCallBackStr, message);
         return env->NewStringUTF("Hello from C++ over JNI!");
 }

//name mapping table
static JNINativeMethod method_table[] = {//6
                { ( char*)"native_wiper_class_init",(char*) "()V", (void *)classInit},
                {(char*)"native_wiper_init",(char*)"()V", (void *)instanceInit},
                {(char*)"stringFromJNI",(char*) "()Ljava/lang/String;", (void*)stringFromJNI}
};

//using name mapping table
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) {
        JNIEnv* env;
        printf("enter  JNI_OnLoad \n");
        if ( vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6 )!= JNI_OK   ){//8
                return -1;
        }
        else {
                jclass clazz = env->FindClass("HelloJNI");
            if(clazz){
                env->RegisterNatives(clazz, method_table, sizeof(method_table)/sizeof(method_table[0]));
                env->DeleteLocalRef(clazz);
                return JNI_VERSION_1_6;
            }else return -1;
        }
   return JNI_VERSION_1_6;
}

//helloJNI.java
public class HelloJNI {
   static {
       System.out.println("HQ loadlibrary hellojni :in JAVA STATIC");
       System.loadLibrary("hellojni");
       native_wiper_class_init();
   }

  // hello.dll (Windows) or libhello.so (Unixes)
   public native String stringFromJNI();
   private native void native_wiper_init();
   private static native void native_wiper_class_init();

  /*callback function*/
   public void callback(String msg) {
         System.out.println("HQ callback:in JAVA");
   }

   // Test Driver
   public static void main(String[] args) {
       System.out.println("HQ forward call in JAVA");
      new HelloJNI().stringFromJNI();  // invoke the native method
   }
}
⚠️ **GitHub.com Fallback** ⚠️