3

I am trying to do some USB programming using the library HID API. Following is my code

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <hidapi.h>


int main(int argc, char* argv[])
{
    int res;
    unsigned char buf[65];
#define MAX_STR 255
    wchar_t wstr[MAX_STR];
    hid_device *handle;
    int i;

    // Enumerate and print the HID devices on the system
    struct hid_device_info *devs, *cur_dev;

    devs = hid_enumerate(0x0, 0x0);
    cur_dev = devs; 
    while (cur_dev) {
        printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
            cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
        printf("\n");
        printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
        printf("  Product:      %ls\n", cur_dev->product_string);
        printf("\n");
        cur_dev = cur_dev->next;
    }
    hid_free_enumeration(devs);

}

I have added following into the Project properties->Configuration Properties->VC++ Directories->Include Directories

C:\Users\yohan\Documents\HIDApi\windows
C:\Users\yohan\Documents\HIDApi\hidapi
C:\Users\yohan\Documents\HIDApi\libusb

When I run my code, I get the following error

Error   6   error LNK2019: unresolved external symbol _hid_free_enumeration referenced in function _main    c:\Users\yohan\documents\visual studio 2010\Projects\USB_Test\USB_Test\FirstTest.obj    USB_Test
Error   7   error LNK2019: unresolved external symbol _hid_enumerate referenced in function _main   c:\Users\yohan\documents\visual studio 2010\Projects\USB_Test\USB_Test\FirstTest.obj    USB_Test
Error   8   error LNK1120: 2 unresolved externals   c:\users\yohan\documents\visual studio 2010\Projects\USB_Test\Debug\USB_Test.exe    USB_Test

Why I am getting this error? I am using Visual studio 2010 professional.

PeakGen
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

3

You've included the headers, so the compiler knows about all the symbol declarations (functions, etc). But, the linker needs to link your usages of these symbols to their actual definitions -- and it can't find them (hence the error).

This can be caused by a variety of things, but in your case it looks like you haven't built the the library, or if you did (or you have a pre-built version), you haven't linked it in (that's what the Library Path directories are for -- you need to add the .lib file as input to the linker, add it will search the library paths for that .lib).

Another way to link in a .lib file is via a compiler-specific (MSVC-only, in this case) #pragma directive:

#pragma comment(lib, "thelibrary.lib")
Community
  • 1
  • 1
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • Thanks for the reply. Unfortunatly, there is no lib. If you don't mind, could you please download it and check please? it is just 1MB – PeakGen May 10 '13 at 17:06
  • @Knight: Since there's no .lib bundled, you need to build the project in order to get one. You can choose to either build it statically (producing a .lib file with all the compiled code in it that gets linked into your code leaving you with a single binary at the end), or build it as a DLL (this is probably the default), which will create a .dll (needed at runtime) and a .lib (which tells the linker everything it needs to know about linking against the DLL). Either way you end up with a .lib for your platform. Let me know if you need more help! – Cameron May 10 '13 at 22:25
0

I have to include their complete visual C++ project into my project. Then the issue is gone

PeakGen
  • 21,894
  • 86
  • 261
  • 463