VS Code: build C / C ++ compiler debugging environment running
1. Background
Usually are compiled using Visual Studio, debug and run C / C ++ program, compile it feels commissioning environment is good, but the body is too large, every time you start to spend a longer time. Only few dozen lines of source code, the size of the VS project to dozens of M, and the new project is relatively time-consuming. So now try to use the Visual Studio Code to build C / C ++ compiler debugging runtime environment to facilitate small C / C ++ program to program.
2. build step
Download and install the VS Code 2.1 Mingw-w64.
VS Code Official Download (https://code.visualstudio.com/Download)
mingw-W64 official download (https://sourceforge.net/projects/mingw-w64/files/mingw-w64/)
Tip:
1. MinGW-W64-install.exe program is installed, click on the download can be installed, but ask your network better, or could not do.
2. i686 32-bit version, x86_64 64-bit version; os is win32 and POSIX interface type; sjlj, seh, dwarf exception handling scheme. Choosing the right operating system version can be, the other for beginners are not very different. After downloading copy directly extract the location you want to install to, note the path without spaces or Chinese.
3. MinGW-W64 need to configure the environment. My Computer> Right select Properties> click Advanced System Settings> click Environment Variables> Click the System Variables path> add the bin address MinGW-W64 (C: \ mingw64 \ bin; ). Remember to click OK to save the settings, use "gcc -v" test has been installed MinGW-W64
2.2 Installation VS Code plug-ins.
Plug | use |
---|---|
C/C++ | To build C / C ++ environment necessary |
C++ Intellisense | IntelliSense plugin supports syntax highlighting part, optional |
Chinese (Simplified) Language Pack for Visual Studio Code | Speaking vs code plug, optional |
Code Runner | Run the program shortcuts plug, optional |
Path Autocomplete | Path automatically prompted to plug, optional |
3. Code Debugging
- Open the folder, the new 1.cpp.
#include <iostream>
int main()
{
int a = 9;
std::cout << a << std::endl;
std::cout << "hello" << std::endl;
std::cout << "你好" << std::endl;
return 0;
}
- Open 1.cpp, click on the "Debug"> "start debugging." Select " C ++ (GDB / LLDB) ", select " G ++. EXE Build and Debug the Active File ", so you can automatically create launch.json.
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
- Open 1.cpp, click on the "Debug"> "Start Debugging" ( Do not debug on launch.json ). At this point the system error, click on the " Configuration Tasks ", select " G ++. Build the Active EXE File ." So the system will automatically create tasks.json.
If " G ++. Build the Active EXE File " does not exist, probably because you debug launch.json, can be converted to 1.cpp re-commissioning.
{
// 有关 tasks.json 格式的文档,请参见
// https://go.microsoft.com/fwlink/?LinkId=733558
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
- Finally, open 1.cpp, click on the "Debug"> "Start Debugging" to commissioning.
4. Run the code (3 methods)
4.1 running in non-debug mode (Ctrl + F5)
Note: which encoding scheme is UTF-8. If replaced GB2312, it may not be output Chinese.
4.2 g ++ command terminal operation
g++ 1.cpp -o 1.exe; ./1.exe
Note: its encoding is GB2312. If replaced UTF-8, the output of Chinese will be garbled.
4.3 Code Runner mounting plug (red rectangle in the drawing) Press the run key
Note: its encoding is GB2312. If replaced UTF-8, the output of Chinese will be garbled.
5. Summary
- Familiar VS Code can not be achieved, when only the built environment to build a basic environment is good, be able to run C / C ++ programs can be compiled debugging, multi-purpose or without it added too many plug-ins installed. VS Code after using more than there is demand, naturally know how to configure the environment, such as shortcuts do not need to remember how, with more naturally know.
- There are many online c_cpp_properties.json, tasks.json, launch.json, the focus is read again. For starters, the system automatically vivid tasks.json and launch.json have basic enough.
- For VS Code Chinese garbled in doubt, refer to VS Code: 4 Ge Chinese garbled problems and solutions .