jaume
jaume

Reputation: 267

JNA undefined symbol

I'm trying to bind the dhcpctl library to Java using JNA. This is mi code (I didn't declare all the functions yet):

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 uses omapi library to call the remote DHCP server. So, when I try to load the library with:

DHCPControlLibrary dhcpExecutor = DHCPControlLibrary.INSTANCE; 

it throws the following error:

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 is an external variable stored in omapi.h. I think I have to do a sort of linking when i load the library, but I don't know how to do it.

Many thanks.

Upvotes: 1

Views: 3356

Answers (3)

Cheer&#39;sLover
Cheer&#39;sLover

Reputation: 31

I think u forget extern "C" while writing c++ code. In my case c++ code:

    #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 ;  
        }   
    }  

and java code

    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);   
        }  
    }  

it works for me

Upvotes: 1

technomage
technomage

Reputation: 10069

Most likely you will need to either explicitly load the omapi library or ensure that it is in LD_LIBRARY_PATH so that the system can find it automatically when the dhcpctl library is loaded.

Upvotes: 0

hhafez
hhafez

Reputation: 39780

omapi_type_generic is not "an external variable stored in omap.h".

This variable must be defined in some .c file somewhere and hence in some .so or .a.

If it is not defined in any .c file then there is your problem right there. Find out why it is so and fix it and you should overcome this exception.

Upvotes: 1

Related Questions