What does MSVC ++ compilation error mean?

I have this compilation error that I do not understand what is wrong. My Microsoft Visual Studio project is a Win32 project (not a console):

1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16 1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals 

EDIT: after creating #include "stdafx.h" as the first line, a compilation error:

 1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16 1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals 

EDIT: hmm, I defined a WinMain function, right? see below code:

 /* Application: */ #include <windows.h> #include <cstdlib> #include "stdafx.h" using namespace std; static HHOOK keyboardHook; static HINSTANCE gInstance; // Functions List // LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ); HHook ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance ); bool DeactivateKeyboardHook( HHook keyboardHook ); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) //int WINAPI WinMain( HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = gInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = L"Custom Class"; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // if registration of main class fails if(!RegisterClassEx(&wc)) { MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, L"Custom Class", L"App Name", WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 600, 500, NULL, NULL, gInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ) { /*if ( code < 0 ) { return CallNextHookEx( NULL, code, wParam, lParam ); }*/ switch ( wParam ) { case WM_KEYDOWN: { MessageBox( NULL, L"Notify", L"Key Down", MB_OK ); } break; case WM_KEYUP: { MessageBox( NULL, L"Notify", L"Key Up", MB_OK ); } break; case WM_SYSKEYDOWN: { MessageBox( NULL, L"Notify", L"Sys Key Down", MB_OK ); } break; case WM_SYSKEYUP: { MessageBox( NULL, L"Notify", L"Sys Key Up", MB_OK ); } break; default: { } break; } return CallNextHookEx( NULL, nCode, wParam, lParam ); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { keyboardHook = ActivateKeyboardHook( &LowLevelKeyboardProc, gInstance ); } break; case WM_COMMAND: { switch(LOWORD(wParam)) { default: break; } } break; case WM_CLOSE: { DeactivateKeyboardHook( keyboardHook ); DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: break; } return DefWindowProc(hwnd, msg, wParam, lParam); } HHOOK ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance ) { return SetWindowsHookEx( WH_KEYBOARD_LL, hookProc, hInstance, 0 ); } bool DeactivateKeyboardHook( HHook keyboardHook ) { return UnhookWindowsHookEx( keyboardHook ); } 
+4
source share
2 answers

This is not a compilation error, it is a linker error, and this means that your program does not define the WinMain function, which is the entry point to the program.

Make sure your program has this feature:

 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow); 

See the MSDN documentation:


 #include <windows.h> #include <cstdlib> #include "stdafx.h" 

If you selected a precompiled header file, then the above is incorrect, stdafx.h should be included at the beginning of the file. Therefore, reorder as follows:

 #include "stdafx.h" //this should be first line of the program! #include <windows.h> #include <cstdlib> 

And I think you do not need to include <windows.h> , since most likely stdafx.h already included it. Check this.

Now why should it be turned on first? Because the precompiled header is, as the name suggests, a precompiled header. The compiler does not compile it every time. Instead, it compiles all the content in it once. If you do not enable it in the first place, the compiler will not know whether the files included before it should be compiled or not, because it may be that these files are already included in stdafx.h and therefore have already been compiled. See this topic:

+6
source

This is not a compilation error, this is a link error. Basically, you use a function that you declared somewhere but never defined in any translation unit.

0
source

Source: https://habr.com/ru/post/894979/


All Articles