Vscode build C / C + + compiler environment detailed tutorial

Time:2020-11-13

Visual studio code (vscode) compiles C++

Visual studio code (vs Code / VSC for short) is a free and open source modern lightweight code editor. It supports syntax highlighting, intelligent code completion, custom hotkeys, bracket matching, code fragment, code comparison diff, GIT and other features of almost all mainstream development languages. It can be described as the work of conscience of Microsoft.

Download and install vs Code

https://code.visualstudio.com/Download

Install cpptools plug-in

Open vs code, press Ctrl + Shift + P to call out the command box, and enter the following command
ext install cpptools

这里写图片描述

Install the MinGW compiling and debugging environment

(if CodeBlocks, dev, C + + etc. are installed on the computer, you can skip this step,
Directly jump to [configure system environment variable path])

http://mingw.org/

Enter the official website and click download installer on the right to download the installer
Open the installer and click Install to prepare for installation

这里写图片描述

Select an installation directory. The default value is C: MinGW. Here, you can select C: MinGW
Click continue to start the installation. The installation process needs to be connected to the network. If an error is prompted during installation, you need to cross the wall

这里写图片描述

Select several required items and right-click make for installation to mark. GCC and G + + are C and C + + compilers
After selecting all the items you want to install, click apply changes under the installation menu in the upper left corner to apply the modification. The process needs to be connected to the network. If there is an error in the middle, you can continue first. If it fails at last, you need to go over the wall and update it. It is recommended to cross the wall.

这里写图片描述

[configure system environment variable path]

Then configure the system environment variable path, which is required
Right click Properties on my computer:
Then follow the steps below. Note that the newly-built item should correspond to the previous installation position of MinGW

这里写图片描述

Open vscode again. After configuring the system environment variable path, restart vscode
Note that vscode debugging needs to be done in the open folder (note! Open folder with VSC!! , open a separate CPP separately is not the following configuration files appear!!
After opening the folder, create a new CPP input code test

这里写图片描述

Enter the debugging interface and select C++

这里写图片描述

Generated in launch.json The configuration is as follows

这里写图片描述

{ 
 "version": "0.2.0", 
 "configurations": [ 
 { 
 "Name": "(GDB) launch", // configuration name, which will be displayed in the drop-down menu of startup configuration 
 "Type": "cppdbg", // configuration type, here can only be cppdbg 
 "Request": "launch", // request configuration type, which can be launch or attach 
 "Program": "${workspaceroot} / ${filebasenamenoextension}. Exe", // the path of the program to be debugged 
 "Args": [], // the command line parameter passed to the program during program debugging is generally set to null 
 "Stopatentry": false, // when set to true, the program will pause at the program entrance, generally set to false 
 "CWD": "${workspaceroot}", // the working directory for debugging programs. Generally, ${workspaceroot} is the directory where the code is located 
 "environment": [], 
 "Externalconsole": true, // whether the console window is displayed during debugging. Generally, it is set to true to display the console 
 "MIMode": "gdb", 
 "miDebuggerPath": "C:\MinGW\bin\ gdb.exe Note that the path of "MinGW" corresponds to "debugger" 
 "Prelaunchtask": "G + +" // a task executed before the start of a debugging session. It is usually a compiler. C + + is G + +, C is GCC 
 "setupCommands": [ 
 { 
					"description": "Enable pretty-printing for gdb", 
  "text": "-enable-pretty-printing", 
  "ignoreFailures": true 
 } 
 ] 
 } 
 ] 
}

Then switch to CPP interface
Press and hold Ctrl + shirft + P to call up the command line interface, enter task and select Run task
You will be asked to build one tasks.json file
Enter the following code

{
 "version": "0.1.0",
 "command": "g++",
 "Args": ["- G", "${file}", "- O", "${filebasenamenoextension}. Exe"], // compile command parameters
 "problemMatcher": {
 "owner": "cpp",
 "fileLocation": ["relative", "${workspaceRoot}"],
 "pattern": {
 "regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$",
 "file": 1,
 "line": 2,
 "column": 3,
 "severity": 4,
 "message": 5
 }
 }
}

Save, press F5 again, run successfully

Configuration C_ cpp_ properties.json

Create a new C in the. Vscode folder_ cpp_ properties.json File for
Enter the following:


{
 "configurations": [
 {
 "name": "Win32",
 "includePath": [
 "${workspaceRoot}",
 "C:/MinGW/lib/gcc/mingw32/5.3.0/include/*",
 "C:/MinGW/include/*"
 ],
 "defines": [
 "_DEBUG",
 "UNICODE",
 "__GNUC__=6",
 "__cdecl=__attribute__((__cdecl__))"
 ],
 "intelliSenseMode": "msvc-x64",
 "browse": {
 "path": [
  "${workspaceRoot}",
  "C:/MinGW/lib/gcc/mingw32/5.3.0/include/*",
		 "C:/MinGW/include/*"
  
 ]
 },
 "limitSymbolsToIncludedHeaders": true,
 "databaseFilename": ""
 }
 ],
 "version": 3
}

At this point, vscode is fully configured.

In addition to vscode, the author also recommends another editor atom. Please refer to my other article for the detailed configuration process

https://www.jb51.net/article/186549.htm

summary

This article about vscode to build C / C + + compilation environment will be introduced here. For more content of vscode C + + compilation environment, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!

Recommended Today

Webassembly and trust

Rust’s support for webassembly is the most perfect. After all, a large part of Mozilla’s original development of trust was to write servo (browser rendering engine). In addition, the two wasm runtimes, wasmer and wasmtime, which we introduced, are also written with rust. For the use of rust to write wasm module and the use […]