差異處
這裏顯示兩個版本的差異處。
java:jna:c_so的undefined_symbol問題 [2017/03/16 11:06] tony [How to resolve?] |
java:jna:c_so的undefined_symbol問題 [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== c++ so的undefined symbol問題 ====== | ||
- | ===== Problem ===== | ||
- | 我有一隻簡單的範例程式如下,我想把它編成so好透過JNA存取: | ||
- | <file cpp test.c> | ||
- | int test() | ||
- | { | ||
- | return 0; | ||
- | } | ||
- | |||
- | </file> | ||
- | 接著透過以下指令把它編成so: | ||
- | <code bash> | ||
- | g++ -shared -fPIC -o libtest.so test.c | ||
- | </code> | ||
- | JNA與測試程式如下: | ||
- | <file java Tester.java> | ||
- | import com.sun.jna.Library; | ||
- | import com.sun.jna.Native; | ||
- | |||
- | public class Tester { | ||
- | |||
- | public interface Test extends Library { | ||
- | Test INSTANCE = (Test)Native.loadLibrary("test", Test.class); | ||
- | |||
- | int test(); | ||
- | } | ||
- | |||
- | public static void main(String[] args) { | ||
- | System.out.println(Test.INSTANCE.test()); | ||
- | } | ||
- | } | ||
- | </file> | ||
- | 在執行後會出現以下錯誤: (請記得要設定環境變數LD_LIBRARY_PATH) | ||
- | <code> | ||
- | Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'test': /opt/test/libtest.so: undefined symbol: test | ||
- | at com.sun.jna.Function.<init>(Function.java:208) | ||
- | at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:536) | ||
- | at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:513) | ||
- | at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:499) | ||
- | at com.sun.jna.Library$Handler.invoke(Library.java:199) | ||
- | at com.sun.proxy.$Proxy0.test(Unknown Source) | ||
- | at com.supermicro.ssm.common.jna.Tester.main(Tester.java:15) | ||
- | </code> | ||
- | ===== How to resolve? ===== | ||
- | 在我透過gcc編成so時,這問題是沒發生過的。到底兩者差在哪呢? 因此我也透過gcc指令去產生so檔來做比較: | ||
- | <code bash> | ||
- | gcc -shared -fPIC -o libtest.so test.c | ||
- | </code> | ||
- | |||