2009-07-30 62 views
1

我試圖使用JNA將dhcpctl庫綁定到Java。這是MI代碼(我沒有申報所有功能尚未):JNA未定義符號

package com.abiquo.abicloud.omapi; 

import com.abiquo.abicloud.omapi.DHCPControlStructure.DHCPCtrlDataString; 
import com.abiquo.abicloud.omapi.DHCPControlStructure.DHCPHandle; 
import com.abiquo.abicloud.omapi.OmapiControlStructure.OmapiObjectTypeT; 
import com.sun.jna.Library; 
import com.sun.jna.Native; 
import com.sun.jna.Pointer; 

/** 
* Binding of the dhcpctl header. 
* @author [email protected] 
*/ 
public interface DHCPControlLibrary extends Library 
{ 
    /** 
    * Create the loaded instance of the native library 
    */ 
    DHCPControlLibrary INSTANCE = 
     (DHCPControlLibrary) Native.loadLibrary("dhcpctl", DHCPControlLibrary.class); 

    /** 
    * Define as synchronized 
    */ 
    DHCPControlLibrary SYNC_INSTANCE=(DHCPControlLibrary)        Native.synchronizedLibrary(INSTANCE); 

    int dhcpctl_initialize(); 
    int dhcpctl_connect (DHCPHandle handle1, String address, int port, DHCPHandle.ByValue handle2); 
    int dhcpctl_wait_for_completion (DHCPHandle handle, Pointer int1); 
    int dhcpctl_get_value (DHCPCtrlDataString dataString , DHCPHandle.ByValue handleValue, String str1); 
    int dhcpctl_get_boolean (Pointer int1, DHCPHandle.ByValue handleValue, String str1); 
    int dhcpctl_set_value (DHCPHandle.ByValue handleValue, DHCPCtrlDataString dataString, String str1); 
    ... etc ... 

} 

dhcpctl使用OMAPI庫調用遠程DHCP服務器。所以,當我嘗試加載庫:

DHCPControlLibrary dhcpExecutor = DHCPControlLibrary.INSTANCE; 

它引發以下錯誤:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'dhcpctl': /usr/lib/libdhcpctl.so: undefined symbol: omapi_type_generic 
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:160) 
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:228) 
    at com.sun.jna.Library$Handler.<init>(Library.java:140) 
    at com.sun.jna.Native.loadLibrary(Native.java:372) 
    at com.sun.jna.Native.loadLibrary(Native.java:357) 
    at com.abiquo.abicloud.omapi.DHCPControlLibrary.<clinit>(DHCPControlLibrary.java:40) 
    at com.abiquo.abicloud.omapi.DHCPexecution.main(DHCPexecution.java:11) 

omapi__type__generic是存儲在omapi.h外部變量。我想我在加載庫時必須做一些鏈接,但我不知道如何去做。

非常感謝。

回答

1

omapi_type_generic不是「存儲在omap.h中的外部變量」。

此變量必須在某個.c文件中的某個地方定義,因此在某些.so或.a中定義。

如果它沒有在任何.c文件中定義,那麼你的問題就在那裏。找出它爲什麼是這樣並解決它,你應該克服這個例外。

0

您很可能需要顯式加載omapi庫或確保其位於LD_LIBRARY_PATH中,以便系統可以在加載dhcpctl庫時自動找到它。

0

我想你在編寫C++代碼時會忘記extern「C」。 在我的情況 C++代碼:

#include <stdlib.h> 
    #include <iostream> 
    using namespace std; 
    extern "C" 
    { 
     void test() { 
      cout << "TEST" << endl; 
     } 

     int addTest(int a,int b) 
     { 
      int c = a + b ; 
      return c ; 
     } 
    } 

和Java代碼

import com.sun.jna.Library; 
    import com.sun.jna.Native; 

    public class jnatest1 { 

     public interface Clibrary extends Library { 
      Clibrary INSTANTCE = (Clibrary) Native.loadLibrary("hello", 
        Clibrary.class); 

      void test(); 
      int addTest(int a,int b);  
     } 

     public static void main(String[] args) { 
      Clibrary.INSTANTCE.test(); 
      int c = Clibrary.INSTANTCE.addTest(10,20);  
      System.out.println(c); 
     } 
    } 

它爲我工作