Kelly O’Hair’s Blog: Compilation of JNI Code

… dynamic loading of native libraries can be tricky. Some of the compilation issues relate to the library being loaded into a Java process and causing conflicts in the way the JDK was compiled itself, or the way the JDK assumes code was compiled (sometimes this is considered a JDK bug, but it depends on a great number of factors).

All libraries loaded into java are assumed to be MT-safe (Multi-thread safe). This means that multiple threads could be executing the code at the same time, and static or global data may need to be placed in critical sections. If you don’t know what MT-safe means, you should do some research and make sure you understand what this means, it is a critical concept to understand when creating JNI libraries and writing JNI code. So if there is a compiler option to select MT-safe generated code, you will need it.

Compiler optimizations are always an issue and you need to be careful with high optimization levels. The safest route is to stick with just -g compilations, then use lowest optimization level you need, increasing it only when your native code and the overall application really benefits from increased optimizations.