(C++) Win32 API 使用 g++ 返回 "undefined reference to `WinMain@16'" 的示例代码
(C++) Win32 API sample code returning "undefined reference to `WinMain@16'" using g++
我正在使用带有 notepad++ 的 G++ 编译器。
我正在学习 windows 的教程,我使用的代码是 windows 教程中的示例代码 (https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program) 我将其粘贴到我的记事本中并尝试编译它,但我收到一条错误消息。
这是代码和消息:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
这是我用来编译的命令(powershell)
PS g++ main.cpp
这是我在尝试编译 (powershell) 时收到的消息
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
如果我尝试将 wWinMain
重命名为 WinMain
,则会出现错误
main.cpp:9:12: error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
In file included from c:\mingw\include\windows.h:44:0, from main.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
如果我尝试使用命令 g++ main.cpp -municode
然后我收到消息
g++.exe: error: unrecognized command line option '-municode'
行
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
我将 wWinMain
更改为 WinMain
并且 我将 PWSTR
更改为 LPSTR
现在可以使用了。
我不知道为什么。希望它能帮助任何遇到同样问题的人(-:
我正在使用带有 notepad++ 的 G++ 编译器。
我正在学习 windows 的教程,我使用的代码是 windows 教程中的示例代码 (https://docs.microsoft.com/en-us/windows/win32/learnwin32/your-first-windows-program) 我将其粘贴到我的记事本中并尝试编译它,但我收到一条错误消息。
这是代码和消息:
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
这是我用来编译的命令(powershell)
PS g++ main.cpp
这是我在尝试编译 (powershell) 时收到的消息
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status
如果我尝试将 wWinMain
重命名为 WinMain
,则会出现错误
main.cpp:9:12: error: conflicting declaration of C function 'int WinMain(HINSTANCE, HINSTANCE, PWSTR, int)' int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
In file included from c:\mingw\include\windows.h:44:0, from main.cpp:5: c:\mingw\include\winbase.h:1263:14: note: previous declaration 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)' int APIENTRY WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
如果我尝试使用命令 g++ main.cpp -municode
然后我收到消息
g++.exe: error: unrecognized command line option '-municode'
行
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
我将 wWinMain
更改为 WinMain
并且 我将 PWSTR
更改为 LPSTR
现在可以使用了。
我不知道为什么。希望它能帮助任何遇到同样问题的人(-: