環境
OS::Microsoft Windows [Version 10.0.17134.285] x64
VSC:Version:1.27.2 (system setup)
VS:2017
心血來潮想使用VSC來寫C++,官方文檔和網上大多資料都是使用g++進行編譯的。這里給出使用MSVC的方案。
參考了《用Visual Studio Code 來生成您的C++應用程序》的方法,當給出的版本太舊,在最新的win10上會有錯誤。
-
在插件界面安裝C++ 開發組件。

-
新建個文件夾,命名為helloworld ,並使用VSC 打開。之后新建helloworld.cpp ,輸入標准的HelloWorld代碼。

-
Ctrl+Shift+P 喚出控制台,執行 C/Cpp: Edit Configurations...命令,初始化工程。

之后會生成.vscode文件夾和c_cpp_properties.json文件。

- 查找vcvarsall.bat(需要安裝Visual Studio)路徑,之后新建一個build.bat(與helloworld.cpp同一路徑),輸入以下指令:
| @echo off call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 set compilerflags=/Od /Zi /EHsc set linkerflags=/OUT:helloworld.exe cl.exe %compilerflags% helloworld.cpp /link %linkerflags% |
注意將vcvarsall.bat路徑替換成你自己的,如果是32位系統,需要將x64替換位x86。

-
Ctrl+,打開Settings界面,搜索 terminal.integrated.shell.windows 修改VSC 默認的命令行為cmd(默認為powershell,使用powershell會出錯)。

-
Ctrl+Shift+P 喚出控制台, Tasks:Configure Default Build Task ,選擇Other模板,新建tasks.json 。

修改為如下參數:

-
Ctrl+Shift+B運行buil task,開始編譯。

順利的話就能在EXPLORER窗口看到編譯后的程序。

使用MSVC編譯的話,調試配置會比較方便。
-
Ctrl+Shift+D進入Debug界面,點擊齒輪,選擇C++(Windows)模板,生成launch.json。

-
修改launch. json中的:program 屬性。

啟動參數可以修改args參數。
-
下斷點,開始調試。

更多調試的操作,可以參考《C/C++ for Visual Studio Code (Preview)》
多文件編譯可以參考《Walkthrough: Compiling a Native C++ Program on the Command Line》,編寫相應的build.bat即可。
參考資料:
用Visual Studio Code 來生成您的C++應用程序
From <https://blogs.msdn.microsoft.com/c/2016/12/20/%E7%94%A8visual-studio-code-%E6%9D%A5%E7%94%9F%E6%88%90%E6%82%A8%E7%9A%84c%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F/>
Walkthrough: Compiling a Native C++ Program on the Command Line
From <https://msdn.microsoft.com/en-us/library/ms235639.aspx?f=255&MSPPError=-2147217396>
C/C++ for Visual Studio Code (Preview)
From <https://code.visualstudio.com/docs/languages/cpp>
