1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Visual Studio Code with WSLでatcoder-cliを使いつつデバッグもできる環境構築

Last updated at Posted at 2021-04-29

まる三日くらいかかった。とりあえず形になったので備忘録兼誰かの参考になればということで。(当方linuxやjsonファイルやそもそもIDEについての知識が全然ないので多分ちゃんとした人が見たら「何だこのコードは…」となるかもしれないですが堪忍してください。やさしくマサカリを投げてもらえるとうれしいです)

はじめに

ファイル構造としては、Cドライブ直下にcodeというフォルダを置き、そのなかにAtCoderというフォルダをおき、更にそのなかに大まかな分類をしました。たとえばabc086のa問題のフォルダをpathをLinux風に書くと、

/mnt/c/code/atcoder/abc051-100/abc086/a

のようになります。僕が作った大まかな分類は以下のとおりです:
image.png

ただし、abc086やそれ以下の階層はatcoder-cli(後述)が自動で作ってくれるので自分で作らなくて大丈夫です。

VSCodeとWSLの導入

このページ通りに設定します。Linuxはなにそれ美味しいの状態なので、WSL-Remoteは使わない設定にしました。

atcoder-cliの導入

このページの通りに設定します。テンプレートの設定はしておくと楽そうなのでしておきました。

デバッグ可能な環境を構築する

まずboostをwsl環境でinstallします:

$ brew install boost

めっちゃ時間かかるし失敗もするっぽいのでうまく行かないときは何度かためしつつ気長に待ちます。

それからこのページの通りに設定します。、、が、ここでなぜか全然うまく行きませんでした(WSL-Remoteを使う前提だからうまく行かなかったのかな?)。色々試行錯誤して以下のようにjsonファイルを書き換えるとそれなりに上手く動きました。

c_cpp_properties.json → そのまま

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            //"options": {
            //    "shell": {
            //        "executable": "/mnt/c/windows/system32/wsl.exe",
            //    }
            //},
            "command": "g++",
            "args": [
                "-std=gnu++1y",
                "-g",
                "-O0",
                "-I/opt/boost/gcc/include",
                "-L/opt/boost/gcc/lib",
                "-o",
                "${fileDirname}/a.out",
                "${file}",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Bash on Windows Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/a.out",
            "pipeTransport": {
                "debuggerPath": "/usr/bin/gdb",
                "pipeProgram": "/mnt/c/windows/system32/bash.exe",
                "pipeArgs": ["-c"],
                "pipeCwd": "/"
            },
            "cwd": "${fileDirname}",
            "args": [
                "<",
                "${fileDirname}/test/sample-1.in",
            ],
            
            "stopAtEntry": false,
            "environment": [],
            "externalConsole": true,
            
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            
            "sourceFileMap": {
                "${workspaceFolder}": "${workspaceFolder}"
            },
        }
    ]
}

tasks.jsonについては、まず"executable"をlinux風のpathに変更し、引数"args"のうち、出力先をそのファイルのディレクトリに変更しました。これでatcoder-cliと併用できるようになります。

launch.jsonについては、ほとんどlinux風のpathに変更しただけです。また、tasks.jsonに合わせて出力ファイルのpathも変更し、入力ファイルはonline-judge-toolsが落としてきたsample-1.inを使っています。sample-1.inをいじれば入力を自分で設定できます(もちろんそのファイルでoj tするとWAになりますが)。

注意点

毎回ubuntuから起動するときに

$ cd /mnt/c/code/atcoder
$ code .

とする!

そうしないとVSCodeがtasks.jsonやlaunch.jsonを認識してくれないっぽいです。(もしかしたら設定でなんとかなるかも…?詳しい方教えて下さい。)

毎回Ubuntuから開くのが面倒なときはこんな感じでVSCodeにプロジェクトをピン留めしておくといいと思います:
image.png

それから、ファイルが多くなってきてmain.cppをエクスプローラーから開くのが面倒なときは、

/mnt/c/code/atcoder/abc051-100/abc051/a$ code main.cpp

のような感じでVSCodeのterminalから開くと楽かもしれないです。

どうでもいいこと(自分用)

ネットワークフォルダはgoogle driveに自動でバックアップできないのでCドライブ直下にcodeをおいてWSLから操作する形にしました。gitとか使えばうまくできるんですかね?

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?