You can create shared object file using below mentioned command.
gcc -shared -fpic -o <so-file-name>.so a.c b.c
on Mac OS X, compile with:
g++ -dynamiclib -flat_namespace myclass.cc -o myclass.so g++ class_user.cc -o class_user
On Linux, compile with:
g++ -fPIC -shared myclass.cc -o myclass.so g++ class_user.cc -ldl -o class_user
References:
C++ Dynamic Shared Library on Linux
Build .so file from .c file using gcc command line
Sample tutorial
Sample code to run .so file using java with commands:
HelloJNI.c
#include <jni.h> #include <stdio.h> #include "HelloJNI.h" JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) { printf("Hello World!\n"); return; }
HelloJNI.java
public class HelloJNI { static { System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes) } // A native method that receives nothing and returns void private native void sayHello(); public static void main(String[] args) { new HelloJNI().sayHello(); // invoke the native method } }
Steps to run above .c file using .java file
javac HelloJNI javah HelloJNI gcc -shared -fpic -o libhello.so -I/usr/java/default/include -I/usr/java/default/include/linux HelloJNI.c java -Djava.library.path=. HelloJNI
g++ mylibrary.cpp -o mylibrary.so -shared -fPIC.-m64maybe?