Search code examples
c++jvmjava-native-interfacejvm-hotspot

why does hotspot jvm use extern "C" in its JNI modules?


Since jvm itself is implemented and built by c++, why does it need to declare extern "C"?

extern "C" is used to generated c-compatible target, why does a c++ jvm need that?


Solution

  • One nice quality of JNI is that you can compile it once and link it to any JVM for that platform. These days that's less of an issue since pretty much all JVMs are based on OpenJDK, but once upon a time there were several JVMs that had completely different implementations. My JNI library compiled on Windows could link to the Sun JVM or the Microsoft JVM. Using "C" linkage ensures compatibility regardless of what compiler I'm using, or what compiler the JVM was built with. The names won't get mangled, the parameters aren't modified, etc.