Compiling Java Native C/C++ Code
- 2 minutes read - 253 wordsWe have faced multiple issues while compiling a native c/c++ code and using it with JNI in our Java application
- How to write a JNI program.
jni_md.h
not found.- Incompatible data types.
- On widows
cygwin1.dll
is required when running Java code on the native library. and more.
Actually we were compiling NLP Solver’s native library (IPOPT) on windows 32 bit and on 64bit linux machine.
Solving the chaos!
Here are the steps we have followed to solve the above issues:
-
We had prepared a POC by following the instructions to write a JNI program.
-
Now to compile C code we need to install a c/c++ compiler for windows/linux. There are many compilers available for each OS. We tried following.
- GNU g++ for linux.
- GNU g++ for window on Cygwin.
-
While compiling c/c++ Java native code we get error “
jnimd.h
” not found.jnimd.h
can be located in the folder/include/win32
, so you need to include win32 folder in the include path of c compilation.-Ic:/JDK5.0/include -Ic:/JDK5.0/include/win32
Similarly we can locate the file in linux.
-
After fixing the last error we got lot of incompatible data type issues. This may be fixed by adding standard parameter to compiler as follows:
-O3 -fomit-frame-pointer -pipe -DNDEBUG -pedantic-errors -Wimplicit -Wparentheses -Wreturn-type -Wcast-qual -Wall -Wpointer-arith -Wwrite-strings -Wconversion -Wno-unknown-pragmas -Wl,--add-stdcall-alias | sed -e s'|-pedantic-errors||'
-
Then on windows we got
cygwin1.dll
not found issue. Cygwin dependencies can be removed by parameter-mno-cygwin
.
We have fixed the compiling issues in NLP Solver’s native library (IPOPT) by making above changes in make
file.