我正在尝试使用JNA将 dhcpctl 库绑定到Java。这是mi代码(我还没有宣布所有的功能):

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 jdevesa@abiquo.com
 */
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中的外部变量。我想在加载库时我必须进行一种链接,但我不知道该怎么做。

非常感谢。

有帮助吗?

解决方案

omapi_type_generic 不是“存储在omap.h中的外部变量”。

此变量必须在某个.c文件中定义,因此在某些.so或.a。

中定义

如果没有在任何.c文件中定义,那么就存在问题。找出原因并解决它,你应该克服这个例外。

其他提示

您很可能需要显式加载omapi库或确保它在LD_LIBRARY_PATH中,以便系统可以在加载dhcpctl库时自动找到它。

我想你忘了extern“C”在编写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);   
        }  
    }  

它对我有用

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top