Wednesday, April 10, 2013

How to get JNIEnv from swig?

%{
#include <stdint.h>
#include "jni.h"
/**
 *  A stash area embedded in each allocation to hold java handles
 */
struct Jalloc {
  jbyteArray jba;
  jobject ref;
};

static JavaVM *cached_jvm = 0;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
{
  cached_jvm = jvm;
  return JNI_VERSION_1_2;
}

static JNIEnv * JNU_GetEnv() {
  JNIEnv *env;
  jint rc = (*cached_jvm)->GetEnv(cached_jvm, (void **)&env, JNI_VERSION_1_2);
  if (rc == JNI_EDETACHED)
     av_log(NULL, AV_LOG_ERROR, "current thread not attached\n");
  if (rc == JNI_EVERSION)
    av_log(NULL, AV_LOG_ERROR, "jni version not supported\n");
  return env;
}
%}

Tuesday, March 12, 2013

how to direct GCC to load library at runtime?

gcc normally register path of dependent library at link time and try to search them at system specific location.
But if library is not located at those locations, then it fails to load.
So to force gcc to link at runtime we can use "-bnoipath".

For example :
 gcc -bnoipath -shared test.o -o test.exe -lxyzlib


this will force test.exe to link with xyzlib.dll at runtime.