Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving hid.lib "unresolved external symbol" linker errors in VC++

As the title suggests, I am having the following linker error:

error LNK2019: unresolved external symbol "unsigned char __stdcall HidD_GetAttributes(void *,struct _HIDD_ATTRIBUTES *)" (?HidD_GetAttributes@@YGEPAXPAU_HIDD_ATTRIBUTES@@@Z)

when calling result = HidD_GetAttributes(WriteHandle, &attributes) in my code.

This function should exist in "hid.lib" which I have added to my linker dependencies for the project. I have also included the header file "hidsdi.h" which has the function prototype for HidD_GetAttributes.

The only other thing that I thought might be problematic is that the function prototypes for "hid.lib" are split between three different headers: hidsdi.h, hidpi.h, and hidsage.h.

Any suggestions?

like image 465
Ben Avatar asked Sep 30 '11 18:09

Ben


People also ask

How do you fix unresolved externals?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do I fix linker errors?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

How do I fix lnk2001?

To fix this issue, build all the object files and libraries with the same version of the compiler before linking them together.

What kind of an error is an unresolved external reference?

Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.


1 Answers

Just solved the problem. Apparently "hid.lib" was written in C which was resulting in some name mangling. Using

extern "C"
{
    #include "hidsdi.h"
}

cleared everything up.

like image 105
Ben Avatar answered Sep 18 '22 15:09

Ben