The Family The Family - 11 months ago 60
C++ Question

GetAsyncKeyState returning fatal link error

I am trying to make a basic console game in c++ and when i try to use GetAsyncKeyState in the following code:

if(GetAsyncKeyState(VK_UP) != 0){
int y2 = (y - 1);
case ' ':{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = 'O';

...but i keep getting the following error when i try to compile it:

error LNK2019: unresolved external symbol __imp__GetAsyncKeyState@4 referenced in function "void __cdecl Draw(void)" (?Draw@@YAXXZ)

fatal error LNK1120: 1 unresolved externals

i am not sure if i need to include any libraries other than iostream and windows.h

Answer Source

The error comes from the linker. You have to link with User32.lib where the GetAsyncKeyState symbol is defined (actually it's an import library for the User32.dll). You can do it either by writing

#pragma comment(lib, "User32.lib")

in your .cpp file, or adding User32.lib to your linker command line.

Note that if you use Microsoft Visual Studio and create a non-empty Win32 project, then the required default Windows system libraries are already linked with. However, since you did not indicate how exactly do you compile and link, I won't expand more on this topic.