无法使用GCC从MSDN编译Win32应用程序示例

无法使用GCC从MSDN编译Win32应用程序示例,c,winapi,gcc,compilation,C,Winapi,Gcc,Compilation,我试着编译 编译命令: mingw32-gcc.exe -D_UNICODE -DUNICODE -DWIN32 -D_WINDOWS -O3 -c "WinMain.c" -o "WinMain.o" mingw32-gcc.exe -D_UNICODE -DUNICODE -DWIN32 -D_WINDOWS -O3 -o "WinMain.exe" "WinMain.o" 以下是来自的错误消息: //GT\u HelloWorldWin32.cpp //使用:/D_UNICODE/DU

我试着编译

编译命令:

mingw32-gcc.exe -D_UNICODE -DUNICODE -DWIN32 -D_WINDOWS -O3 -c "WinMain.c" -o "WinMain.o"
mingw32-gcc.exe -D_UNICODE -DUNICODE -DWIN32 -D_WINDOWS -O3 -o "WinMain.exe" "WinMain.o"
以下是来自的错误消息:

//GT\u HelloWorldWin32.cpp
//使用:/D_UNICODE/DUNICODE/DWIN32/D_WINDOWS/c编译
#包括
#包括
#包括
#包括
//全局变量
//主窗口类名称。
静态TCHAR szWindowClass[]=“win32app”);
//显示在应用程序标题栏中的字符串。
静态TCHAR szTitle[]=“Win32导游应用程序”);
HINSTANCE hInst;
//转发此代码模块中包含的函数声明:
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
int WINAPI WinMain(HINSTANCE HINSTANCE,
HINSTANCE HPPrevenstance,
LPSTR lpCmdLine,
国际展览(nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc=WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_应用程序));
wcex.hCursor=LoadCursor(空,IDC_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+1);
wcex.lpszMenuName=NULL;
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_应用程序));
如果(!RegisterClassEx(&wcex))
{
MessageBox(空,
_T(“调用RegisterClassEx失败!”),
_T(“Win32导览”),
无效);
返回1;
}
hInst=hInstance;//将实例句柄存储在全局变量中
//CreateWindow的参数解释如下:
//szWindowClass:应用程序的名称
//szTitle:出现在标题栏中的文本
//WS_OVERLAPPEDWINDOW:要创建的窗口类型
//CW_使用默认值,CW_使用默认值:初始位置(x,y)
//500、100:初始尺寸(宽度、长度)
//NULL:此窗口的父级
//NULL:此应用程序没有菜单栏
//hInstance:WinMain的第一个参数
//NULL:此应用程序中未使用
HWND HWND=CreateWindow(
szWindowClass,
szTitle,
WS_重叠窗口,
CW_USEDEFAULT,CW_USEDEFAULT,
500, 100,
无效的
无效的
hInstance,
无效的
);
如果(!hWnd)
{
MessageBox(空,
_T(“调用CreateWindow失败!”),
_T(“Win32导览”),
无效);
返回1;
}
//ShowWindow的参数解释如下:
//hWnd:从CreateWindow返回的值
//nCmdShow:WinMain的第四个参数
展示窗口(hWnd,
nCmdShow);
更新窗口(hWnd);
//主消息循环:
味精;
while(GetMessage(&msg,NULL,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}
//
//功能:WndProc(HWND、UINT、WPARAM、LPARAM)
//
//用途:处理主窗口的消息。
//
//WM_绘制-绘制主窗口
//WM_DESTROY-发布退出消息并返回
//
//
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
PAINTSTRUCT-ps;
HDC-HDC;
TCHAR问候语[]=“你好,世界!”;
开关(信息)
{
案例WM_油漆:
hdc=开始喷漆(hWnd和ps);
//这是你的申请表。
//在本简介中,我们只需打印“你好,世界!”
//在左上角。
文本输出(hdc,
5, 5,
问候语;
//结束特定于应用程序的布局部分。
端漆(hWnd和ps);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(hWnd、message、wParam、lParam);
打破
}
返回0;
}
如何解决这个问题?任何帮助都将不胜感激。

请查看


空值应该类似于MB_OK

您必须将库传递给链接器。Visual Studio会自动执行此操作,但您的调用缺少这些标志。您必须添加类似于
-L/lib/w32api
-L/lib/Gdi32
的内容。您必须通过阅读文档来了解这一点,因为我没有使用编程窗口。

这只是一个警告。问题在于链接器,而你的答案与此无关。这不是标题所说的。无论如何,要解决链接器问题,只需添加gdi32库。您可能还需要添加内核32。从技术上讲,不是编译失败,而是链接失败。如果你将程序输出作为复制文本发布,而不是发布一个难以阅读的图像,那会更好;现在可以完美地编译它了+从我这里得到1。
// GT_HelloWorldWin32.cpp
// compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>

// Global variables

// The main window class name.
static TCHAR szWindowClass[] = _T("win32app");

// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("Win32 Guided Tour Application");

HINSTANCE hInst;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL,
            _T("Call to RegisterClassEx failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    hInst = hInstance; // Store instance handle in our global variable

    // The parameters to CreateWindow explained:
    // szWindowClass: the name of the application
    // szTitle: the text that appears in the title bar
    // WS_OVERLAPPEDWINDOW: the type of window to create
    // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
    // 500, 100: initial size (width, length)
    // NULL: the parent of this window
    // NULL: this application dows not have a menu bar
    // hInstance: the first parameter from WinMain
    // NULL: not used in this application
    HWND hWnd = CreateWindow(
        szWindowClass,
        szTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 100,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (!hWnd)
    {
        MessageBox(NULL,
            _T("Call to CreateWindow failed!"),
            _T("Win32 Guided Tour"),
            NULL);

        return 1;
    }

    // The parameters to ShowWindow explained:
    // hWnd: the value returned from CreateWindow
    // nCmdShow: the fourth parameter from WinMain
    ShowWindow(hWnd,
        nCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello, World!");

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);

        // Here your application is laid out.
        // For this introduction, we just print out "Hello, World!"
        // in the top left corner.
        TextOut(hdc,
            5, 5,
            greeting, _tcslen(greeting));
        // End application-specific layout section.

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }

    return 0;
}