How to use VSCode write C / C ++ code? (Win10 x64 systems)

To form good habits, point a praise walk

Have questions, please private letters, comments, and I will see to reply


  1. Download VSCode of
    the country, if not through special methods VSCode official website download speed is very slow;
    you can also use my offer blue cloud playing links: VSCodeUserSetup-x64-1.43.0.exe download;
    if you can not meet the above requirements on the network to download the articles on VSCode dazzling selection of their own right;
  2. A directory, the directory two: Install the plug-ins and compiler
  3. C code written in C VSCode the need to establish a work area
  4. C ++ code written in C ++ VSCode the need to create a workspace
  5. Contents Five: explain the C / C ++ Debug operation of


My programming of the road like most people, is to start by the university in the first programming course "C language"

At that time, the teacher recommended that two lightweight C compiler: Dev C ++, Visual C ++ 6.0, both the C language editor is really easy to use, but

  1. VC 6.0 is 1998 old products, a variety of compatibility issues in today's popular operating system, I started to abandon it
  2. Dev C ++ for the novice to learn C language is a good tool, but it started after by C language, because it provides the functionality is really limited, I can not meet the growing demand, I like a man, like going to slag her abandoned
  3. Later, the network learned that there is a very powerful C language standard IDE: Visual Studio, but this version too much stuff, a huge body mass, a C language learners just started using Visual Studio like artillery to fight mosquitoes, is not necessary; also like a small fart Wazi the masters, powerless; Visual Studio really made it was just started programming my eyes, dizziness, then eventually returned to the arms of Dev C ++

Until today, Microsoft's VSCode increasingly popular, after trying to download and found the experience very good;

VSCode Although only built in JavaScript, TypeScript and Node.js, but it has a very rich language of the other plug-in extension system

It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and runtimes (such as .NET and Unity).

Then plan to use VSCode configure the C / C ++ environment, I occasionally meet the needs of the C programming language, but also the record of this process, we hope to help

A directory: Microsoft C / C ++ extension

  1. Open VSCode
  2. Click on the sidebar Manage extensions icon
    Here Insert Picture Description
  3. Search C / C ++ , click install to

In the C / C ++ for Visual Studio Code (Preview) , clearly illustrate this plug-in does not include the C ++ compiler or debugger, you need to download the C ++ compiler (unless you already have on your computer C ++ compiler)

The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.

Directory two: GCC via Mingw-w64 on Windows

Step One: Download Mingw-w64

For Window users, the official recommended two C ++ compiler (supplement: C ++ compatible C):

I chose to download the GCC Via the Mingw-W64 ON Windows ,

Suppose installed:D:\mingw-w64

Step Two: Configure environment variables

Right this computer> Properties> Advanced System Settings> Advanced> Environment Variables

Select Path, click Edit

Click OK also repeatedly oh, just open the pages to turn off all by determining

I assume that the installation path is: D:\mingw-w64, the environment variable configured for:D:\mingw-w64\bin

Step 3: Check

win + r, input cmd, enter the command at the console, respectively,

g++ --version
gdb --version

So you can explain, the download is successful (if you do not see the expected output, or g ++ or gdb command is not recognized, that failure can ask me in the comments section)

Contents Three: build C ++ workspace

One: Create a new folder for storing c ++ code you write, the position to decide, is not recommended on the desktop

I direct a folder named C ++

Two: Open the folder C ++ with VSCode

  1. The most crisp way: Select the folder> right-click> open by Code

  2. If VSCode not registered to your right-click menu, you honestly open VSCode> file> open folder

Three: to create a c ++ file in the workspace C ++, to ensure the success of subsequent operations

Four: generating tasks.json file

tasks.json file calls g ++ compiler, create an executable file (exe file) based on source code (c ++ file)

The main menu, select Terminal> Configure Default Build Task

In the drop-down list, lists a variety of predefined C ++ compiler build tasks,

Select g ++. Exe build active file

then, VSCode will automatically generate tasks.json file in the workspace, folder on .vscode
Here Insert Picture Description
general, need to add encoding settings in args array, or Chinese output is garbled

"-fexec-charset=GBK"

Four: compile c ++ Code

Suppose I want to run test.cpp

  1. Back test.cpp window
    Here Insert Picture Description
  2. Terminal> Run Build Task
    executable file

    and integrated terminal appears to the bottom panel in the code editor
    Here Insert Picture Description

Five: the implementation of the code c ++

VSCode run executable files, VSCode will prompt you to install PowerShell plug-in, follow the prompts to install

Here high probability encounter a problem: Could not load file ****** ps1, since the prohibition in this system execute the script.
You can look at an article I found, clear explanations and gives the reasons to solve Method

  1. In the integrated terminal panel, open the PowerShell terminal
    Here Insert Picture Description
    Click + to

  2. In a terminal .\test.exe
    Here Insert Picture Description
    Enter

Six: one-time compile multiple C ++ source files

Generated by default tasks.json file, one can only compile the current C ++ source file,

The tasks.json file: args array "${file}"instead ${workspaceFolder}\\*.cpp, can all C ++ source files in one-time compiler workspace

Contents Four: C establish a work area

The above-described operation is repeated a very high degree

  1. Either repeat the operation, but to choose gcc.exe build active file
    Here Insert Picture Description
  2. Either directly modify two values ​​of C ++ workspace file tasks.json

"label": "g++.exe build active file"Instead "label": "gcc.exe build active file";

"command": "mingw64路径\\bin\\g++.exe"Changed"command": "mingw64路径\\bin\\gcc.exe"

mingw64 path: the path to your installation mingw64, remember to write double slash

Contents five: Debug

A: generation launch.json file

Start GDB debugger

Open VSCode, Run> Add Configuration ...
select C ++ (GDB / LLDB)
Here Insert Picture Description

  • If C workspace , select the drop-down list: gcc.exe Build and Debug the Active File
    Here Insert Picture Description
  • If C ++ workspace , select the drop-down list: . G ++ EXE Build and Debug the Active File

So, VSCode will automatically generate launch.json file in the workspace, folder on .vscode
Here Insert Picture Description

  • By default, C ++ extensions do not add any breakpoints in the source code,
    the "stopAtEntry"value to truethe debugger start-stop on the main method of debugging
  • "cwd": "${workspaceFolder}"When the debugger working directory for the workspace folders; change the value ${fileDirname}can be changed to the directory where the file

Here Insert Picture Description

Contents Six: Simple talk about Microsoft C / C ++ extension of Details

Click VSCode plug-in management C / C ++
Here Insert Picture Description
Here Insert Picture Description

  • This extension is an open source project on the github
  • Repository , plug the warehouse on github
  • AND DELINQUENCY , plug-issue forum on github, you can submit bug, submit solutions
  • Documentation document, in fact, point to the official document on this page is VSCode extended sub-page description of plug-ins
  • Code Samples code examples, we can see the new features through plug github

This sample is a simple C++ program that computes and outputs the volume of a box.
We use this example in our blog posts to illustrate new extension features.

  • Offline Installers offline installer, the name may be difficult to understand, in fact, is to install the plug-in VSCode in failed, please go directly to the github download the plug-in, manually install the fight VSCode

Here Insert Picture Description
Visual Studio Live Share is a plug-in is being expanded, and skip

Then Overview and getting started, Quick links, Questions and feedback, is the refinement of the above-mentioned website navigation

Offline installation, teach you how to manually install this plug-in

Microsoft Open Source Code of Conduct, Microsoft's open source guidelines

Data and telemetry, this plugin will collect user data passed to Microsoft to improve the user experience, anyway, I do not care

This extension collects usage data and sends it to Microsoft to help improve our products and services.


Reference article:

Published 156 original articles · won praise 434 · views 210 000 +

Guess you like

Origin blog.csdn.net/Zhangguohao666/article/details/104963520