VC++コマンドラインでコンパイル

# コンソールアプリ

先ずcl.exeへのpathや、include/lib環境変数を調べる。
[スタートメニュー]->[Visual Studio 2022]->[x64 Native Tools Command Prompt for VS 2022]を起動。

```
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.12.1
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

C:\Program Files\Microsoft Visual Studio\2022\Community>where cl
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\bin\Hostx64\x64\cl.exe

C:\Program Files\Microsoft Visual Studio\2022\Community>where $include:stdio.h
C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt\stdio.h

C:\Program Files\Microsoft Visual Studio\2022\Community>where $lib:LIBCMT.lib
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433\lib\x64\libcmt.lib
```

https://yaya.lsv.jp/where/

```dev.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433
path %path%;%vcdir%\bin\Hostx64\x64

set include=%vcdir%\include
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt

set lib=%vcdir%\lib\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64

prompt $e[33m$p$g$e[m
cmd
```

```fig1.c
// cl fig1.c
#include <stdio.h>

int main(void)
{
	printf("hello, world\n");
}
```

```
C:\Projects\msvc>cl fig1.c
Microsoft(R) C/C++ Optimizing Compiler Version 19.42.34433 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

fig1.c
Microsoft (R) Incremental Linker Version 14.42.34433.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:fig1.exe
fig1.obj

C:\Projects\msvc>fig1
hello, world
```

# Windowアプリ

```fig2.c
// cl /MD fig2.c
#pragma comment(lib, "user32")

#include <windows.h>

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MessageBox(NULL, "hello, world", "title", MB_OK);
}
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/257554/fc63f900-53e4-355e-13d4-5d08023efb2d.png)

# ダイアログ

```dev.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433
path %path%;%vcdir%\bin\Hostx64\x64
path %path%;C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64

set include=%vcdir%\include
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt
set include=%include%;C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um

set lib=%vcdir%\lib\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64
set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64

prompt $e[33m$p$g$e[m
cmd
```

```fig3.c
// rc fig3.rc
// cl /MD fig3.c fig3.res
#pragma comment(lib, "user32")

#include <windows.h>
#include "fig3.h"

INT_PTR DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) {
	case WM_COMMAND:
		switch (LOWORD(wParam)) {
		case IDCANCEL:
			EndDialog(hDlg, IDCANCEL);
			return TRUE;
		}
		break;
	}
	return FALSE;
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_HOGE), NULL, DlgProc);
}
```

```fig3.h
#define IDD_HOGE	100
```

```fig3.rc
#include <windows.h>
#include "fig3.h"

IDD_HOGE dialog 100, 100, 120, 50
style WS_OVERLAPPEDWINDOW
caption "test"
begin
ltext	"hello, world", -1, 10, 10, 100, 10
end
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/257554/b1053e72-6329-6312-a5e3-80af1c0ad41b.png)

# MASM

```dev.bat
@echo off
set vcdir=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.42.34433
path %path%;%vcdir%\bin\Hostx64\x64

set lib=%lib%;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\um\x64

prompt $e[33m$p$g$e[m
cmd
```

```fig4.asm
comment *
ml64 /c fig4.asm
link /subsystem:console /entry:start fig4
*
includelib kernel32

extern ExitProcess :proc

	.code
	public start
start:
	sub	rsp, 40
	mov	ecx, 42
	call	ExitProcess
	end
```

```
C:\Projects\msvc>ml64 /c fig4.asm
Microsoft (R) Macro Assembler (x64) Version 14.42.34433.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: fig4.asm

C:\Projects\msvc>link /subsystem:console /entry:start fig4
Microsoft (R) Incremental Linker Version 14.42.34433.0
Copyright (C) Microsoft Corporation.  All rights reserved.


C:\Projects\msvc>fig4

C:\Projects\msvc>echo %errorlevel%
42
```