Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

VSCodeからVisual Studioのコードをビルドできるようにする

Posted at

はじめに

こちらの記事は「大分高専 Advent Calendar 2024」14日目の記事です

皆さんは学校の課題などでVisual Studioを使わされた時にVisual Studioが使いづらいと感じたことはないでしょうか.IDEなので普通に起動に時間がかかることは結構あります.
VSCodeで完結させたいという人も少なからずいるかもしれないのでVSCodeで最低限ビルド,デバッグができるようにしていきます.

環境

今回はVisualStudio2019で作成したc++のプロジェクトのビルドが行えるようにしていきます.

.vscodeに入れるファイルの作成

tasks.json

tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe",
            "args": [
                "/t:build",
                "/p:Configuration=debug",
                "/p:Platform=x64",
                "-m"
                ]
        },
        {
            "label": "build for x64 release",
            "type": "shell",
            "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe",
            "args": [
                "/t:rebuild",
                "/p:Configuration=release",
                "/p:Platform=x64",
                "-m"
            ]
        },
        {
            "label": "rebuild",
            "type": "shell",
            "command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe",
            "args": [
                "/t:rebuild",
                "/p:Configuration=debug",
                "/p:Platform=x64",
                "-m"
            ]
        }
    ]
}

ざっくり説明します.
参考リンクのところに乗せてはいるのですが,MSBuildというものを使ってビルドを行います.
タスクを何個か作ってリビルドとかリリースにも対応しておくようにします.多分もっとキレイに書けますがきっとそのうち追記します.

launch.json

デバッグの構成です.typeはcppvsdbgにします.programはx64向けにビルドしたものであれば上のように書いてください.多分もっとキレイに書けます.preLaunchTaskにはtasks.jsonに先ほど書いたデバッグ用にビルドするタスク名を書いておきましょう.

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name":"build and debug",
            "type":"cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/x64/Debug/{exeファイルの名前}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console":"integratedTerminal",
            "preLaunchTask": "build",
        }

    ]
}

setting.json

このままでもビルドを行うという使命は十分果たせるのですが,例えばNuGetなどで入れたパッケージを認識してくれなくて赤い波線が出てきてイラっとしてしまうこと請け合いなのでincludePathを追加します.

あと,Visual Stuidoの設定に合わせて文字コードの設定をしておきましょう.多分shiftJISだと思います.

setting.json
{
    "C_Cpp.default.includePath": [
        "packages\\{自分で入れたパッケージへのパス}",
        "packages\\DxLib.vc140.3.2.4.1\\build\\native\\include"//例
    ],
    "files.encoding": "shiftjis"
}

おわりに

現在では割とVisual Studioも様々な更新がされているため以前のような使いづらさはないかもしれません.ですが,これによってVSCodeでしか使えない拡張機能等も使えたりするのはメリットだと思います.

前日に寝落ちしてしまい遅れてしまったため気が向いたらちょくちょく追記します.
申し訳ございません

参考リンク

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?