1
2

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 1 year has passed since last update.

vscode, c/c++, python, docker, online judge toolsの環境構築

Last updated at Posted at 2020-11-30

dockerをAtCoderの環境構築に使おう!

目的

AtCoderにて使用するプログラミング環境をdockerで構築する。
使用するPCを変更、またはOSの再インストールなどに柔軟に対応できる。
dockerfileの記載方法を学ぶ。
自分でdockerを使い開発環境を配布できる知識をつける。

これまでの記事

Docker, java, vscodeの開発環境を構築
AtCoderで記載したコードをgithubへアップロードし勉強記録として保存しよう

対象のPC環境

macbook pro

使用するプロジェクト

online-judge-tools
vscode-remote-try-cpp

online-judge-toolsはpython3とpip3を使用します。
python3とpip3をdocker fileとdevcontainer.jsonに記載します。

docker file

vscode-remote-try-cppの.devcontainer > Dockerfileを開きます。
pip3のインストールを行います。

dockerfile
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.134.0/containers/cpp/.devcontainer/base.Dockerfile
ARG VARIANT="buster"
FROM mcr.microsoft.com/vscode/devcontainers/cpp:0-${VARIANT}
# python3-pip
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
    && apt-get -y install --no-install-recommends python3-pip \
    && rm -rf /var/lib/apt/lists/* \
    && rm -rf ~/.cache/pip

devcontainer.json

ここではvscodeで使用するパッケージを指定できます。
python3を使用するので「ms-python.python」を指定します。

extensions
    "extensions": [
        "ms-vscode.cpptools",
        "ms-python.python"
    ],

postCreateCommandにて、
pip3を使用しrequirements.txtに記載されたツールをインストールします。

postCreateCommand
"postCreateCommand": "pip3 install -r requirements.txt",

requirements.txt

requirements.txtにて「online-judge-tools」を指定します。

requirements.txt
online-judge-tools

tasks.json

argsにてC++14を指定します。
「${relativeFile}」で現在開いてアクティブになっているファイルをコンパイルするように指定します。
また、online-judge-toolsの「oj t」にて指定するのは「a.out」の為、コンパイル時に出力するバイナリを「a.out」に指定します。
これで「oj t」をターミナルに入力するだけでサンプルケースをテストできます。

tasks.json
            "args": [
                "-std=c++14",
                "-g",
                "${relativeFile}",
                "-o",
                "a.out"
            ],

launch.json

デバッグ実行時の設定を行います。
「a.out」を実行するように指定。
また入力値を「input.txt"」を使用するようにします。
デバッグ実行の度にINPUT値をコピペしなくてよくなります。

launch.json(C++設定)
            "program": "${workspaceFolder}/a.out",
            "args": ["<input.txt"],

ここにpython3のデバックを起動した際の設定を追加します。
「Python: Current File」にて、作業中のファイルを実行します。
C++と同じように「args」の設定をする事で「input.txt」の値を使用するようにします。

launch.json(python)
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "<input.txt"
            ],
        },

online judge toolsでやっておくこと

online judge toolsのログインのコマンドを実行し、ログインを行いましょう。
本番中のコンテストはログインをしないとコマンドが実行できません。

ログインのcmd
oj login 

まとめ

今まで勉強したターミナル、コマンドプロンプト、docker、vscodeの知識を使用しました。
実際の開発でもdockerとgitは必須知識です。
基礎はできるようにしておきましょう。

次回

vscodeはnpmスクリプトを覚えるともっと便利に使えます。
npmスクリプトを勉強していきます。

1
2
0

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?