プログラマーのメモ書き

伊勢在住のプログラマーが気になることを気ままにメモったブログです

WSL 上に C/C++ の開発環境を構築

WSL 上に C/C++ の開発環境を構築した際のメモです。

ubuntu のバージョンは 18.04.5 です。

以下を参考にしました。

VSCode で C/C++ を書く環境を用意するメモ (Ubuntu)

WSL へ GCC / GDB をインストール

すでに入っていました。

mor@DESKTOP-H6IEJF9:~$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mor@DESKTOP-H6IEJF9:~$

なければ、 build-essential をインストールすればOKです。

mor@DESKTOP-H6IEJF9:~$ sudo apt install build-essential

build-essential をインストールすると gdb も入るとある記事もありますが、手元の環境だと gdb はインストールされていなかったので、別途入れます。

mor@DESKTOP-H6IEJF9:~$ sudo apt install gdb

インストールできたことを確認しておきます。

mor@DESKTOP-H6IEJF9:~$ gdb -v
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
mor@DESKTOP-H6IEJF9:~$

問題ないですね。

VSCode でリモート開発するので、VSCode 用の C/C++ 用の拡張機能をインストールしておきます。

f:id:junichim:20210610103157p:plain

なお、インストール先は、 WSL 側になるので、その点ご注意ください。

ビルドして実行

では、早速試してみます。

VSCode で WSL側の適当なフォルダを開いて、『Hello World』 と表示する C のプログラムのファイルを作ります。

#include <stdio.h>

int main() {

    printf("Hello World\n");

    return 0;
}

この状態で、メニューの『実行』->『デバッグの開始』を選択すると

f:id:junichim:20210610103522p:plain

『環境の選択』をするように表示されるので、

f:id:junichim:20210610103604p:plain

『C++(GDB/LLDB)』を選択します。引き続き、『構成の選択』をするように表示されるので

f:id:junichim:20210610103709p:plain

『gcc - アクティブファイルのビルドとデバッグ』を選択しておきます。

すると、画面下部にターミナルが立ち上がり、コンパイル・実行がおこなわれて、結果が表示されます。

f:id:junichim:20210610104034p:plain

とりあえず、これで最低限の環境は整ったようです。