VS code is a cross-platform editor developed by Microsoft, with a rich plug-in capability to encode, compile and debug a variety of programming languages. Since vs code itself is just an editor, you need to prepare a tool chain for compilation. This article is for Windows systems, and the C + + language development recommendation Mingw-w64 as a toolchain on the Windows platform.
1. Download Install MINGW-W64, and add mingw-w64 Bin directory to environment variable, install mingw-64 tutorial Many, no longer repeat. For example, the environment variables I added are: C:\Program files\mingw-w64\x86_64-7.3.0\mingw64\bin
2. download and install vs code on Microsoft's official website, currently the latest version is v.1.22.2 (2018/4/21)
3. Open vs Code, search for C + + in the extension store on the left, install Microsoft's official plugin, restart vs code, or click "Reload" next to take effect
4. Create a new folder on disk "HelloWorld", vs Code Open this folder, new Main.cpp
#include <iostream>using namespace STD; int main () {cout << " hello World " << Endl; for (int i = 0 ; i < 100 ; i++ ) {cout << i << Endl; return 0 ;}
Then you will find #include <iostream> this line has a wavy line below, prompting you not to find iostream this file, so cannot contain, move the mouse to this line will appear a small yellow light bulb, click the small bulb edit Includepath Setting
Navigate to the location of the Win32, edit the Includepath, and add your header file to the path.
If you do not know the header file path, you can use the command to print the path of the header file:
gcc -v-e-X C + +-
Once added, this is the case:
Now the # include <iostream> will be able to be included properly.
5. Compile: Use the shortcut key CTRL + Shift +B compile main.cpp, because it is the first time you compile, you will be prompted to configure the build task:
{ // Seehttps://go.microsoft.com/fwlink/?LinkId=733558 //For the documentation about the Tasks.json format "version":"2.0.0", "Tasks": [ { "label":"Build", "type":"Shell", "Command":"g++", "args": [ "- G",//indicates that debugging information is added to the generated executable, and if you want to debug the program, the-g parameter is necessary"main.cpp" ], "Presentation": { "reveal":"never" }, "Problemmatcher": [ "$GCC" ] } ]}
Use the shortcut key again CTRL + Shift +b to compile the main.cpp, compile to see the generated a.exe file, which is the default file name compiled generated, you want to customize the file name can use the-o parameter.
6. Debug:After the A.exe file is generated, click on the Debug button on the left:
Display no configuration, click to add the configuration, modify the completion remember to save:
{ //use IntelliSense to understand related properties. //hover to view the description of an existing property. //For more information, please visit:https://go.microsoft.com/fwlink/?linkid=830387 "version":"0.2.0", "configurations": [ { "name":"gdb C + +", "type":"cppdbg", "Request":"Launch", " Program":"${workspacefolder}/a.exe", "args": [], "Stopatentry":false, "CWD":"${workspacefolder}", "Environment": [], "Externalconsole":true, "Mimode":"gdb", "Midebuggerpath":"C:/Program Files/mingw-w64/x86_64-7.3.0/mingw64/bin/gdb.exe", "Setupcommands": [ { "Description":"Enable pretty-printing for GDB", "text":"-enable-pretty-printing", "Ignorefailures":true } ] } ]}
Click the Debug button again to see that the program stopped at the point where I set the breakpoint so that we can debug our own program. The left side of the layout shows the value of the variable, stack information, and thread information.
VS code for C + + development