金魚亭日常

読書,ガジェット,競技プログラミング

VS Code (Windows) で C++ の環境設定

Visual Studio は個人的に入れたくないので,Microsoft Visual C++ Build Tools 2015 を入れる.

VS Code にcpptools を入れて設定をする.

code.visualstudio.com

C/C++ extension for Visual Studio Code | Visual C++ Team Blog からダウンロードできるhelloworld のサンプルを見ると良い.

build.bat を書いて,

@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64     
set compilerflags=/Od /Zi /EHsc
set linkerflags=/OUT:hello.exe
cl.exe %compilerflags% helloworld.cpp /link %linkerflags%

Ctrl + Shift + Bコンパイルすると,tasks.json がないと言われて,.vscode フォルダの下に作られる.

{  
   // See https://go.microsoft.com/fwlink/?LinkId=733558
   // for the documentation about the tasks.json format
   "version": "0.1.0",
   "windows": {
      "command": "build.bat",
      "isShellCommand": true,
      "showOutput": "always"
}

そのあとは Ctrl + Shift + Bコンパイルできるようになる.

clang-format でコードフォーマットできるらしいので入れるのも良い.

"clang" C Language Family Frontend for LLVM

clang を入れると,clang++ もしくは clang-cl を使えるようになるので,そっちでもいいと思う.

その場合は(PATHを通して),

@echo off
clang++ -I "c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include" %~1

という感じで build.bat を書いて,build hello.cpp って風にやる.

補完とかのために,.vscode/c_cpp_properties.json ができるので(#include のとこにカーソル合わせるとできた),そこにclang-format の設定も書く.

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": ["/usr/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        },
        {
            "name": "Linux",
            "includePath": ["/usr/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        },
        {
            "name": "Win32",
            "includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include"],
            "browse" : {
                "limitSymbolsToIncludedHeaders" : true,
                "databaseFilename" : ""
            }
        }
    ], 
    "clang_format": {
      "style": "file", 
      "fallback-style": "LLVM", 
      "sort-includes": false
    }
}

2017-05-03 追記

開発者コマンドプロンプト以外でclang++使う場合,環境変数の設定が必要です

what-alnk.hatenablog.com