Hola, trato de crear una ventana básica de Windows, pero sin ningún IDE, estoy utilizando el compilador de MinGW y el código es el siguiente.
#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 hPrevInstance, 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);
}
Yo solo quiero compilarlo desde consola con el comando g++ .\ventana1.cpp -o ventana1.exe
pero como el código no posee función main() me da el error:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to `WinMain@16'
Me gustaría saber como se puede compilar el programa desde consola, si necesito agregar otros parámetros o se tiene que compilar de una forma distinta
Compilar ya lo compilaste, te dio un error al enlazar, no encuentra el simbolo WinMain@16, por alguna razon tu WinMain tiene una w al principio wWinMain. Ya veo que ese codigo te funcionara en VC++ donde la RTL se hace cargo de llamar a wWinMain con la linea de comandos en wide char pero el punto de entrada del programa recibe como parametro una cadena ANSI.
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain
https://stackoverflow.com/questions/3571250/wwinmain-unicode-and-mingw/11706847
Usa -municode al compilar como dicen ahi arriba.