When php editor Youzi is writing a C program, sometimes he may encounter the problem of "the compiled C library function cannot be found". This error usually occurs during the linking phase and means that the compiler cannot find the required library function. There may be many reasons for this problem, such as incorrect configuration of the library file path, improper installation of the library file, etc. In order to solve this problem, we can fix it by checking the library file path, reinstalling the library file, and updating the compiler. In this article, we'll detail how to fix this common compilation error.
I have a file named libmylibrary.a and a file named mylibrary.h The header file contains a function named myfunction(). I combined them with the cpp file (helloworldjni.cpp) and created a new library called native.dll. However, when I try to compile native.dll in java using jni, I cannot find the functions in libmylibrary.a and mylibrary.h.
mylibrary.h
// mylibrary.h #ifndef mylibrary_h #define mylibrary_h #ifdef __cplusplus extern "c" { #endif void myfunction(); // declare the function prototype #ifdef __cplusplus } #endif #endif // mylibrary_h
These are the steps I followed
1. Create a java file for integrating jni (helloworldjni.java)
public class helloworldjni { static { system.load("/home/centos/test5/native.dll"); } public static void main(string[] args) { new helloworldjni().sayhello(); } //native method with no body public native void sayhello(); }
2.Use this command to create the helloworldjni.h file
javac -h . helloworldjni.java
3. Integrate the mylibrary.h file and helloworldjni.h file with my cpp file (helloworldjni.cpp)
#include<iostream> #include<jni.h> #include "mylibrary.h" #include "helloworldjni.h" void sayhello(){ myfunction(); } int main() { sayhello(); // call the function from the library return 0; } jniexport void jnicall java_helloworldjni_sayhello (jnienv* env, jobject thisobject) { sayhello(); }
4. Compile these files (create .o files)
g++ helloworldjni.cpp -l. -lmylibrary -i"/home/centos/jdk-21.0.1/include" -i"/home/centos/jdk-21.0.1/include/linux" -o helloworldjni.o
5. Generate .o file to .dll file
g++ -shared -o native.dll helloworldjni.o
6. Compile and run helloworldjni.java
java helloworldjni
Received this error:
java: symbol lookup error: /home/centos/test5/native.dll: undefined symbol: myFunction
How do I incorporate libmylibrary.a into my java code compilation to ensure it recognizes the functions in libmylibrary.a?
ps: I'm sure there's no problem with libmylibrary.a since I'm able to call myfunction() using pure c but there seems to be some kind of linking issue when using jni that I can't figure out
I tried all your steps with some minor changes and it worked for me. Attached build script:
#!/bin/sh JAVA_SDK_HOME=/home/user/lib/jdk-21.0.1 #echo BUILD THE LIB #g++ -c mylibrary.cpp -o mylibrary.o #ar rcs libmylibrary.a mylibrary.o echo MAKE JAVA STUB $JAVA_SDK_HOME/bin/javac -h . HelloWorldJNI.java echo BUILD SO file g++ -c -fPIC -o HelloWorldJNI.o HelloWorldJNI.cpp \ -I $JAVA_SDK_HOME/include/ \ -I $JAVA_SDK_HOME/include/linux g++ -shared -o native.dll HelloWorldJNI.o -L. -lmylibrary echo TESTING $JAVA_SDK_HOME/bin/java HelloWorldJNI
But there are some things to note:
jniexport
is actually __declspec(dllexport)
rather than __declspec(dllimport)
*.so
files on linux you need to compile the c file using
-fpicThe above is the detailed content of Compiled c++ library function not found. For more information, please follow other related articles on the PHP Chinese website!