※当サイトの記事には、広告・プロモーションが含まれます。

Visual Studio Code で、C言語の実行環境を構築、とりあえずは、ターミナルで動かせるところまで

C言語を一度は経験しとかねばとは思っていたのですが、なかなか踏ん切りが付かずにいましたが、Visual Studio Code で動かせるらしいんだとか。

gabekore.org

kutimoti.hatenablog.com

⇧  上記サイト様を参考にさせていただきました。

 

というわけで、C言語の環境構築にレッツ、トライ。

Visual Studio Code をインストールされてない方は、インストールしちゃいましょう。

 

まずは、C言語コンパイラ

言わずとしれた、C言語コンパイラーである、gcc 。この方がいないとC言語のプログラムが動かないって感じっすかね?

で、Windowsの場合は、

の2パターンが導入しやすいようです。おそらく、Visual Studio 2015 以降ならcl.exeは導入されてるのではないかと。

 

今回は、MinGWgcc.exe を利用していく感じで。

 

MinGWをインストール

では、Chocolatey を利用してインストールしちゃいましょう。

コマンドプロンプトを管理者権限で起動して、mingwのパッケージを表示。

f:id:ts0818:20181002204934p:plain

⇧  mingw 5.3.0 がインストールできるみたいです。

とりあえず、今回は、「C:¥chocolatey-package」というディレクトリにインストールしようと思うので、ディレクトリを作成しておきます。

f:id:ts0818:20181006123741p:plain

で、インストールを試みるも、何回やってもC:\tools に勝手にインストールされてしまう...何でやね~ん!

Path provided by the Get-BinRoot helper - will be deprecated later (closer to v1) but okay to use for now

 

The path returned by the helper Get-BinRoot can be used as the parent directory for the installation. Get-BinRoot will return the value of the environment variable %ChocolateyBinRoot%. If the value does not contain a drive reference, the system drive will be prepended. If the environment variable is not set, the default path (C:\ToolsC:\Chocolatey\bin) will be returned.

As an example, MinGW uses %ChocolateyBinRoot%. If the environment variable is not set, it will be set to c:\tools and MinGW will install to C:\Tools\MinGW by default. If %ChocolateyBinRoot% is set to "C:\Common\bin", MinGW installs to C:\Common\bin\MinGW.

%ChocolateyBinRoot% gives the Chocolatey user a way of controlling where packages are installed. If you want to allow customizing the installation path, then this is currently the way to go.

https://chocolatey.org/docs/create-packages#installation-paths

⇧  何でも、packageによっては、環境変数「ChocolateyBinRoot」が設定されてない場合は、問答無用でpackageのデフォルトのインストール先が指定されてしまうらしい...

で、MinGWもその一つらしい。

stackoverflow.com

⇧  他にもRubyなんかのpackageもそうらしい。

環境変数を見てみたら、

f:id:ts0818:20181006122716p:plain

⇧  見事に、 「ChocolateyBinRoot」なんてものは存在せず...

作成します。「新規(N)...」で。

f:id:ts0818:20181006122929p:plain

今回は、「C:¥chocolatey-package」っていうディレクトリにpackageがインストールされるようにします。「OK」で。

f:id:ts0818:20181006123358p:plain

「OK」で。

f:id:ts0818:20181006123826p:plain

 

それでは、コマンドプロンプトを管理者権限で立ち上げ直して、インストール。 

choco install mingw --version 5.3.0 -y -o -ia "'/qn /norestart ALLUSERS=1 TARGETDIR=C:\chocolatey-package'"

f:id:ts0818:20181006125444p:plain

f:id:ts0818:20181006124434p:plain

f:id:ts0818:20181006131228p:plain

⇧  相変わらず、packageのインストール場所が変わらんのだが...

superuser.com

You can set environment variable ChocolateyBinRoot to C:\. In newer versions of Chocolatey (v0.9.10+) this is now called ChocolateyToolsLocation.

How to install cygwin using chocolatey to specific directory? - Super User

⇧  上記サイト様によりますと、Chocolateyのバージョンが0.9.10 以上だと、「ChocolateyToolsLocation」が利用されるらしい...「ChocolateyBinRoot」は過去の遺産になってしまったわけですな。

というわけで、インストールされたpackageをアンインストール。「choco uninstall [package名]」コマンドの失敗率が高いので、手動で削除。

f:id:ts0818:20181006132430p:plain

f:id:ts0818:20181006132532p:plain

一応、キャッシュっぽいやつも削除しときましょう。

「C:¥Users¥ユーザ名¥AppData¥Local¥Temp¥chocolatey」におるはず。「mingw」ってフォルダを削除。

f:id:ts0818:20181006133357p:plain

 

なんか、コマンドプロンプトの場合は、 

  • If it was an MSI, then usually you could pass -ia "INSTALLDIR=""D:\Program Files""" (for cmd.exe, it's different for PowerShell). See how to pass options/switches for specifics on passing quoted values through.

https://chocolatey.org/docs/getting-started#overriding-default-install-directory

⇧  オプションの指定の仕方が違うっぽい?

やってみた。

f:id:ts0818:20181006140853p:plain

f:id:ts0818:20181006140952p:plain

⇧  まったく変わらず... 

一時的に環境変数を変更して、試みるも、

f:id:ts0818:20181006141746p:plain

f:id:ts0818:20181006141843p:plain

⇧  頑なに、C:¥tools にインストールしてくるですけど~。 

環境変数を直接、変えないといけないっぽい。

f:id:ts0818:20181006142529p:plain

で、実行。

f:id:ts0818:20181006142723p:plain

f:id:ts0818:20181006142834p:plain

f:id:ts0818:20181006143010p:plain

⇧  一応、インストール先を変えれたけど、mingwのパッケージについては、インストール先のディレクトリをoverrideするのは無理そうですね。 

「C:¥MinGW」にインストールできてないのが気になりますが、パスを通しておきます。

f:id:ts0818:20181006152355p:plain

「新規(N)」を選択。

f:id:ts0818:20181006152446p:plain

「mingw64¥bin」までのパスを追加。

f:id:ts0818:20181006152646p:plain

 

まぁ、今回の本筋ではないので、MinGWについては、このへんで。 

 

Visual Studio CodeC言語を使えるよう設定

Visual Studio Code を起動。「拡張機能」のアイコン f:id:ts0818:20181006144134p:plain をクリック。 

f:id:ts0818:20181006144037p:plain

検索欄に、「C/C++」と入力して、検索にヒットした「C/C++」をインストール。

f:id:ts0818:20181006144307p:plain

「再読み込み」をクリック。

f:id:ts0818:20181006144720p:plain

インストールした拡張機能が有効になります。

f:id:ts0818:20181006144709p:plain

 

では、適当にプロジェクト用のディレクトリを作成。

f:id:ts0818:20181006154643p:plain

Visual Studio Codeで開きます。「ファイル(F)」>「フォルダを開く(F)...」を選択。

f:id:ts0818:20181006154804p:plain

先程作成した「cHello」を選択、「フォルダーの選択」で。

f:id:ts0818:20181006155028p:plain

ディレクトリ名にカーソルを持っていって表示される f:id:ts0818:20181006155331p:plain をクリック。

f:id:ts0818:20181006155225p:plain

拡張子が「.c」のファイルを作成。

f:id:ts0818:20181006155434p:plain

ファイルを編集。

roads-pro.com

⇧ 上記サイト様を参考にさせていただいてます。

 
#include <stdio.h>

int main(){
    
    printf("Hello World!\n");
    puts("Hello World!");
    printf("%s", "Hello World!\n");
    
    return 0;
}    

そしたらば、「表示(V)」>「ターミナル(T)」を選択。

f:id:ts0818:20181006155819p:plain

「ターミナル」で、コンパイルを実行し、hello.exeという実行ファイルを作成。

gcc -o hello hello.c

f:id:ts0818:20181006160601p:plain

実行ファイルが作成されたので、実行します。「ターミナル」に、 

./hello

と入力。

f:id:ts0818:20181006161140p:plain

⇧  Hello World! が3行、表示されました。

 

Visual Studio Code拡張機能で、「Runner」または「Code Runner」というものを導入すれば、「ターミナル」でいちいちコンパイルなどしなくても実行できるようになるみたいですが、どちらの拡張機能を使うべきかがよく分からないので、しばらくは、「ターミナル」でやっていく感じで。

 

とりあえず、C言語が実行できる環境はできたようです。C言語も、少しづつ勉強していきたいですね。

今回はこのへんで。