Visual Studio Code---介绍

0 Preface/Foreword

1、安装VScode

官网:Download Visual Studio Code - Mac, Linux, Windows

文档:Documentation for Visual Studio Code

1.1 优点

  1. Intelligent code completion: code smarter with intellisense - completions for variables, methods, and imported modules.
  2. Streamlined debugging: Print debugging is a thing of the past. Debug in VS Code with your terminal tools
  3. Fast, Powerful Editing: Linting, multi-cursor editing, parameters hints, and other powerful editing features
  4. Code Navigation and refactoring: Browse your source code quickly using peek and navigate to definition
  5. In-product source control: speed up your release cycle with SCM support inside your editor, including rich Git integration

1.2 添加扩展插件

1.2.1 添加C/C++extension

下载市场,marketplace:C/C++ - Visual Studio Marketplace

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, macOS. When you create a *.cpp file, the extension adds features such as syntax highlighting (colorization ),smart completions and hovers (IntelliSense), and error checking.

安装扩展步骤:

  1. 打开VS Code(Open VS Code)
  2. 选择在活动条中的扩展功能的可视图标(Select the Extension view icon on the Activity bar or use the keyboard shortcut(ctrl+shift+X))
  3. 搜索 C++(Search for 'C++')
  4. 选择安装(Select Install)

1.2.2 设置C++环境(set up C++ environment)

C++ is a compiled language meaning your program's souce code must be translated (compiled) before it can be run on your computer. The C/C++ extension doesn't include a C++ compiler or debugger, since VS Code as an editor relies on command-line tools for the development workflow. You need to install these tools or use those tools already installed on your computer.

检查你是否已经安装了编译器(Check if you have a compiler installed)

Note: There may already be a C++ compiler and debugger provided by your academic or work development environment. Check with your instructors or colleagues ofr guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).

Common compilers that already come preinstalled on some platforms are the GNU Compiler Collection (GCC) on Linux and the Clang tools with Xcode on macOS.
Common compilers that already come preinstalled on some platforms are the GNU Compiler Collection (GCC) on Linux and the Clang tools with Xcode on macOS.

To check if you already have them installed:

  1. Open a new VS Code terminal window using (++Ctrl+Shift+`++)

  2. Use the following command to check for the GCC compiler g++:

g++ --version

or this command for the Clang compiler clang:

clang --version
The output should show you the compiler version and details. If neighter are found, make sure your compiler executable is in your platform path (%PATH on Windows , %PATH on Linux and macOS) so that the C/C++ extension can find it. Otherwise, use the instructions in the section below to install a compiler.

打开终端命令行窗口

shift+ctrl+`

使用命令:

g++ version
The output should show you the compiler version and details. If neither are found, make sure your compiler executable is in your platform path (%PATH on Windows, $PATH on Linux and macOS) so that the C/C++ extension can find it. Otherwise, use the instructions in the section below to install a compiler.

安装MSYS2

注意:安装过程结束后,一定要选择运行,否则无法加载mingw-w64-ucrt相关工具。

Example: Install MinGW-x64 on Windows

To understand the process, let's intall MinGW-w64 via MSYS2. MinGW-w64 is a popular, free toolset on Windows. It provides up-to-date native builds of GCC, Ming-w64, and other helpful C++ tools and libraries.

  1. Download using this direct link to the MinGW installer.

  2. Run the installer and follow the steps of the installation wizard.Note, MSYS2 requires 64 bit Windows 8.1 or newer.

  3. In the wizard, choose your desired installation Folder. Record this directory for later. In most cases, the recommended directory is acceptable. The same applies when you get to setting the start menu shortcuts step. When complete, ensure the Run MSYS2 now box is checked and select Finsh. A MSYS2 terminal window will then automaticaly open.

  4. In this terminal, install the MinGW-w64 toolchain by running the following command:

pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain

  1. Accept the default number of packages in the toolchain group by pressing Enter.

  2. Enter Y when prompted whether to proceed with the installation.

  3. Add the path to your Min-GW-w64 bin folder to the Windows PATHenvironment variable by using the following steps:

a. In the Windows search bar, type Settings to open your Windows Settings.

b. Search for Edit environment variables for your account.

c. In your User variables, select the Path variable and then select Edit.

d. Select New and add the MinGW-w64 destination folder you recorded during the installation process to the list.If you selected the defaut installation steps, the path is:

c:\msys64\ucrt64\bin.

e. Select OK to save the updated PATH. For the new PATH to be available, reopen your console windows.

  1. Check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type

You should see output that states which versions of GCC, g++ and GDB you have installed. If this is not the case, make sure your PATH entry matches the Mingw-w64 binary location where the compiler tools are located

通过MSYS2安装MinGW64

配置环境变量

验证mingw-64工具安装完成并且可用

在VS Code自带的terminal下run commands: gcc --version & g++ --version separately.

1.2.3 Create a Hello World App(创建一个Hello World应用)

++参考文档++ :C++ programming with Visual Studio Code

To make sure the compiler is installed and configured correctly, lets create a Hello World C program.

Create a C file:

  1. On Windows, launch a Windows command prompt (Enter Windows command prompt in the Windows search bar). On macOS and Linux, you can enter these commands in the terminal.

  2. Run the following commands. They are creating an empty folder called projects where you can place all your VS Code projects. The next commands create and navigate you to a subfolder called helloworld. From there, you are opening helloworld directly in VS Code using the code command.

mkdir projects

cd projects

mkdir helloworld

cd helloworld

code .

The "code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept the Workspace Trust dialog by selecting Yes, I trust the autors since this is a folder you created.

Now create a new file called helloworld.c with the New File button in the file Explorer or File > New File command

After you finish edit source code, please press Ctrl+S to save the file. You can also enable AutoSave to automatically save your file changes, by checking Auto Save in the main File menu.

通过命令行(command prompt)创建项目和工程,并在工作路径下打开VS Code,打开命令code .

第一次使用VS Code打开工程时,会提醒是否信任,点击++tick in check box++。

在工程界面新建文件并命名。

在写source code的时候,VS Code会进行++代码补齐++。(属于IntelliSense功能)

编译和运行程序:在右上方选择编译工具链并运行

Run helloworld.c

  1. Make sure you have helloworld.c open so it is the active file in your editor.

  2. Press the play button in the top right corner of the editor.

  3. Choose C/C++ from the list of detected compilers on your system.

You are only prompted to choose a compiler the first time you run helloworld.c. This compiler becoms "default" compiler set in your tasks.json file.

  1. After the build succeeds. you should see "Hello World" appear in the integrated Terminal.

运行完成之后,程序中的打印信息会显示在terminal窗口。

编译生产可在Windows执行的文件,extension name is ++exe++。

可执行文件可以在windows中的git bash下执行。

1.2.4 VS Code相关文件

VS Code 打开工程之后,会在工作路径的根目录下产生一个++.vscode++的folder,该folder下面包含两个文件:

  • launch.json
  • tasks.json
相关推荐
不戳的柠檬18 小时前
解决vscode不能像pycharm一样从其他同级文件夹导包
ide·vscode·pycharm
khatung21 小时前
React中事件绑定和Vue有什么区别?
前端·javascript·vue.js·vscode·react.js·前端框架
一条破秋裤1 天前
针对git、gitee&VSCode连接的使用 || Live Share插件使用
git·vscode·gitee
去你的鸟命2 天前
vscode 关闭绑定元素 隐式具有“any”类型这类错误
ide·vscode·编辑器
旺仔学IT2 天前
VSCode设置
vscode·编辑器·工具使用技巧
ahadee2 天前
蓝桥杯每日真题 - 第15天
c语言·vscode·算法·蓝桥杯
啊森要自信2 天前
【linux学习指南】VSCode部署Ubantu云服务器,与Xshell进行本地通信文件编写
linux·服务器·ide·vscode·ubuntu
moneyxjj2 天前
vscode中执行git合并操作需要输入合并commit信息,打开的nano小型文本编辑器说明-
ide·git·vscode
susu10830189112 天前
vscode执行npm install报错
ide·vscode·npm