-1

I must use main() and call a function from windows.h.

The following code wants a WinMain() function to be used instead of main().

#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    int vk_Shift = 16;

    while (1)
    {
        if (GetAsyncKeyState(vk_Shift) < 0)
        {
            printf("Shift is pressed\n");
        }
    }
}

Error

Error   1   error LNK2019: unresolved external symbol _WinMain@16 
referenced in function ___tmainCRTStartup   

Error   2   error LNK1120: 1 unresolved externals   

How can I get this work in VS2013?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1
    I'm not using VS for C code, but I guess configuring it as "console" application should do the trick. –  May 11 '17 at 16:50
  • 1
    Why "must" you use main()? But, as @FelixPalmen points out, console applications use "main" as their entry point and GUI applications (by default) use WinMain. I don't know if there is a setting, perhaps you just need to recreate the app as a console app. Or just change the Linkers /SUBSYSTEM flag from WINDOWS to CONSOLE. – Chris Becke May 11 '17 at 16:50
  • @ChrisBecke, I am writing a console application. – user366312 May 11 '17 at 16:51
  • Doing good for me on VS2010.... tested on a empty project. – Pavan Chandaka May 11 '17 at 16:52
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) and [Unresolved external symbol _WinMain@16](http://stackoverflow.com/q/5259714/62576) and about three dozen others asking about *Unresolved external symbol _WinMain@16* – Ken White May 11 '17 at 16:53
  • "*The following code wants a `WinMain()` function to be used instead of `main()`*" - the **code** doesn't care what entry point you use. It is all the same from the API's perspective. The **project** cares. You are clearly compiling the code in a GUI project, rather than a console project. You need to adjust your project settings accordingly. – Remy Lebeau May 11 '17 at 18:03

1 Answers1

0

Okay, guys, I got it.

Felix Palmen's advice works.

... ... I guess configuring it as "console" application should do the trick.

So, what I did is, I changed my project's preference from WIDOWS to CONSOLE.

enter image description here

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452