C 言語手習い:Visual Studio

作成日:2015-08-30
最終更新日 :

導入されていた Visual Studio

C# か F# を勉強しようとして、Visual Studio を入れていた。 そうしたら、C の環境も入っていた。動かしてみよう。
C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/

ここの bin/cl.exe がコンパイルプログラムである。 では、適当な場所に hello.c を作り、動かしてみよう。

cl hello.c
'cl' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。

きっと C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin が環境変数 PATH に入っていないのが原因だろう。 環境変数 PATH にこれを追加する。

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

hello.c
hello.c(1) : fatal error C1034: stdio.h: インクルード パスが設定されていません。

それでは環境変数 INCLUDE を作り、設定する。

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

>hello.c
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
LINK : fatal error LNK1104: ファイル 'LIBCMT.lib' を開くことができません。

それでは、環境変数 LIB を作り、その中に、LIBCMT.lib が入っているフォルダを指定した。

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

hello.c
Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj
LINK : fatal error LNK1104: ファイル 'kernel32.lib' を開くことができません。

このように、見つからなかったり開けなかったりするファイルがあったとき、 そのつど調べて対処を繰り返すのは面倒だ。調べた結果、
C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/Tools/vsvars32.bat
というバッチファイルがあり、これを実行することで解決した。

C++ 対応状況

VC++ の C++ 規格対応状況(www.ruche-home.net) にある通りだ。 2013 年版を使っていたのだが、右辺値参照 && ができなかったり、constexpr が定義されていなかったりで困った。

まりんきょ学問所コンピュータの部屋C言語手習い > Visual Studio


MARUYAMA Satosi