LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem compiling a libusb program (https://www.linuxquestions.org/questions/programming-9/problem-compiling-a-libusb-program-915642/)

Sneezie 11-26-2011 10:56 AM

Problem compiling a libusb program
 
Dear coders,
I am trying to compile a C program that includes the following code:

Code:

#include "libusb.h"

#define VENDOR_ID  0x0DE7
#define PRODUCT_ID 0x0191

#define CANT_SEND -1
#define CANT_READ -2


static struct usb_device *find_U401( struct usb_bus *bus )
    {

    struct usb_device *dev;

    /* look through all busses */
for (; bus; bus = bus->next )
        {
        /* look at every device  */
        for ( dev = bus->devices; dev; dev = dev->next )
            {
            /* match to known IDs  */
            if ( dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct == PRODUCT_ID )
                {
                return dev;
                }
            }
        }
    return NULL;

}

[...]

I get from the gcc compiler the following error messages:

Quote:

usb@.c:10:45: warning: 'struct usb_bus' declared inside parameter list [enabled by default]
usb@.c:10:45: warning: its scope is only this definition or declaration, which is probably not what you want [enabled by default]
usb@.c: In function 'find_U401':
usb@.c:16:22: error: dereferencing pointer to incomplete type
usb@.c:19:24: error: dereferencing pointer to incomplete type
usb@.c:19:49: error: dereferencing pointer to incomplete type
usb@.c:22:21: error: dereferencing pointer to incomplete type
usb@.c:22:62: error: dereferencing pointer to incomplete type
The program is given by USBMicro as an example program to control under linux their circuits.

I have managed to get control under Windows, but that works invoking a DLL, a different story. I need to switch to linux, and actually in the end crosscompile it for an embedded PowerPC based linux system. But at the moment, in order to go in steps, just compiling for testing in a full Linux PC would be a progress...

I hope I have worked correctly the include dependencies with libusb, but the libusb test program compiles and runs fine.

I am a very basic C programmer, I am more in Fortran (!), Basic, Matlab environments and I am not fully understanding this program structure I have not written myself, therefore after many attempts and googleing I feel a bit helpless at the moment and I hope in a kick forward from you!

I understand it should lie in some missed variable declaration, but this were I stop...

I thank you in advance for any help!

Sneezie

firstfire 11-27-2011 01:16 AM

Hi.

What linux distribution do you use? How did you install libusb?
In Ubuntu you can just
Code:

sudo apt-get install libusb-dev
After that this program (which looks similar to yours) compiles if you change #include to
Code:

#include <usb.h>
`usb.h' is the only header file that comes with `libusb-dev':
Code:

$ dpkg -L libusb-dev  | grep .h$
/usr/include/usb.h

Don't forget to link your program against libusb:
Code:

gcc test.c -lusb
Hope this helps.

Sneezie 11-27-2011 05:17 AM

Thank you firstfire! :)

Quote:

Originally Posted by firstfire (Post 4534992)

What linux distribution do you use? How did you install libusb?

I am using Ubuntu 11.
Actually, I have already installed libusb, both by apt-get (as a second smarter move) and (firstly) by compiling the downloaded distribution (can this have messed things a bit?).

It seems it got installed in /usr/local/lib, which I added to lib.so.conf and then I run ldconfig.

I was actually including libusb, since with usb I got referencing error messages for the send_message command. And then in the search of some solution I tried to include libusb and got stuck with it, since it was also compiling fine the example program of the libusb library.

Your suggestion has therefore solved the original problem I posted, but now if I include lines including the "send_message" command, which are in the rest of the program I have not posted to the forum, I get the errors:

Code:

usb@.c:(.text+0x1fb): undefined reference to `send_command'
The other commands related to USB management in the test program seem to compile fine when I take away the `send_command' statements.

Therefore, it seems this time the problem lies in the dependencies to libusb which are not correctly addressed.
You managed to compile the program from the USBMicro site, if I understood well, therefore the problem is on my side, I reckon...
I am (have been) working on that, but if you have a hint, I would be grateful!

I bet the crosscompiling step will be even tougher (I already managed to crosscompile with buildroot other programs on a couple of platforms, but libusb so far is the one giving me headaches... I am not 'that' experienced and when things get bumpy for any reason I get lost).

Thank you again! :)

Sneezie

firstfire 11-27-2011 05:57 AM

Hi.

Could you please post complete code and gcc command line?

Your last error message looks strange because function `send_command' is defined in the code I compiled:
Code:

int send_command( struct usb_dev_handle *handle, char *command, int comLen, int resLen )
{

    int ret = usb_control_msg( handle, 0x21, 9, 0x0200, 0, command, comLen, 5000 );

    // check that send was successful
    if ( ret != comLen )
        return CANT_SEND;

    // does the command expect a result?
    if ( resLen > 0 )
        {
        ret = usb_bulk_read( handle, 0x81, command, resLen, 5000 );
        if ( ret != resLen )
            return CANT_READ;

        }

    return ret;

}


Sneezie 11-27-2011 06:35 AM

Quote:

Originally Posted by firstfire (Post 4535101)
Hi.
Your last error message looks strange because function `send_command' is defined in the code I compiled:

Bingo!

I was trying compiling the code in chunks and I forgot to put the send_command definition back in place! :redface:

Obviously the compiler was complaining! :doh:

Compilation successful!

On to the crosscompiler now!

I think I tried all the combinations, but once I got in trouble I began to swirl in nonsense...
You put all pieces in the right order!

Thank you really very much!

Sneezie :)

P.S.-Unfortunately this is not a promise that I will not get into trouble again...


All times are GMT -5. The time now is 05:06 PM.