Windowsでコンパイルしてみる

※VisualStudioはインストールした状態。

コンソールアプリの場合

サンプルのプログラムを作成。

test.c

#include <stdio.h>
 
int main()
{
    printf("Hello, world!\n");
    return 0;
}

Developer Command Prompt for VS 2019
コマンドプロンプトを起動し、clコマンドでコンパイル

> cl test.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.28.29334 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

exeファイルが作られ、実行できた。

> test.exe
Hello, World!

Windowsアプリの場合

testw.c

#include <windows.h>

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    MessageBox(NULL, "Hello, world!", "Test", MB_OK);
    return 0;
}

MessageBoxを呼ぶにはuser32.libが必要になるが、
Express Editionにはuser32.libは無いらしく、

下記のページから「Windows 10 SDK バージョン 2004 (10.0.19041.0)」をインストールしてみる。

Windows SDKエミュレーターアーカイブ
https://developer.microsoft.com/ja-jp/windows/downloads/sdk-archive/

これで、コンパイルできるようになり、

> cl testw.c user32.lib
Microsoft(R) C/C++ Optimizing Compiler Version 19.28.29334 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

testw.c
Microsoft (R) Incremental Linker Version 14.28.29334.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:testw.exe
testw.obj
user32.lib

実行すると、メッセージボックスが表示された。

> testw.exe

f:id:yk5656:20210208090639j:plain