1

我正在尝试使用 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 中的外部变量。我想我必须在加载库时进行某种链接,但我不知道该怎么做。

非常感谢。

4

3 回答 3

1

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

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

如果它没有在任何 .c 文件中定义,那么你的问题就在那里。找出原因并修复它,您应该克服此异常。

于 2009-09-15T00:40:24.693 回答
0

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

于 2011-05-06T12:50:11.597 回答
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);   
        }  
    }  

这个对我有用

于 2017-06-09T05:46:50.503 回答