VSCode环境配置 for C and C++

前期准备

此配置教程参考了VSCode官方Doc,目的只为在Windows上利用VSCode编译运行C和C++。

安装VSCode

VSCode官方下载链接
下载安装过程和QQ微信类似,无需详细解释。

安装C/C++ extension

在插件视图(Ctrl+Shift+X)内搜索C++,找到下图的扩展并安装。

安装Mingw-w64

Mingw-w64能够提供gcc环境,至于gcc是什么,可以在Web上寻找答案。我个人的理解是:gcc是计算机将c语言转换为可执行的exe的工具。
具体安装步骤如下

  • 1.下载Mingw-w64
  • 2.运行Mingw-w64,并在架构选项中选择X86_64
  • 3.选择默认安装配置即可,先复制下默认的安装路径,接下来配置环境变量需要用到
  • 4.打开系统环境变量Path一项,将上述复制的路径添加进去
  • 5.打开cmd并测试“g++ -v”与“gcc -v”这两条命令是否有反馈(若没有反馈的话说明操作有问题,官方文档参考如下)

Install Mingw-w64 via the SourceForge website. Click Mingw-w64 to download the Windows Mingw-w64 installer.

  • Run the installer.
  • For Architecture select x86_64 and then select Next.
  • On the Installation Folder page, use the default installation folder. Copy the location as you will need it later.
  • Select Next to start the installation.

Add the path to your Mingw-w64 bin folder to the Windows PATH environment variable by using the following steps:

  • In the Windows search bar, type 'settings' to open your Windows Settings.
  • Search for Edit environment variables for your account.
  • Choose the Path variable and then select Edit.
  • Select New and add the Mingw-w64 destination folder path to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the > path: C:Program Filesmingw-w64x86_64-8.1.0-posix-seh-rt_v6-rev0mingw64in.
  • Select OK to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.

正式配置

创建工作区

所谓的工作区,就是一个文件夹,可在桌面新建一个VS_Code文件夹用于测试(一般创建这种文件夹我都不会用中文,避免麻烦)

创建cpp/c

用VSCode打开刚刚的文件夹,并在里面新建一个用于测试的c或者cpp

创建task.json(build instructions)配置文件

VSCode面板最上方有一排功能区,找到Terminal > Configure Default Build Task,然后按照下图选择gcc

可以看到图中gcc的路径就是在前期准备过程中配置安装的,如果环境变量设置错误了,这一步就不会出现gcc。
选择gcc之后,VSCode会自动创建.vscode文件夹,并将task.json配置文件保存在此目录。

其实这个task.json文件就是用来配置gcc的,gcc会把c程序编译成exe文件,官方文档说明如下,看不懂的可以在Linux环境下练习gcc相关的命令就会明白啥意思了。

The command setting specifies the program to run; in this case that is g++. The args array specifies the command-line arguments that will be passed to g++. These arguments must be specified in the order expected by the compiler. This task tells g++ to take the active file (({file}), compile it, and create an executable file in the current directory (){fileDirname}) with the same name as the active file but with the .exe extension (${fileBasenameNoExtension}.exe), resulting in helloworld.exe for our example.

创建launch.json(debugger settings)配置文件

此文件是用来启动GDB调试器的,当你F5开始调试程序的时候,它就会起到作用。

  • 1.在主菜单找到Run > Add Configuration,选择带有GDB的选项即可

  • 2.在接下来的选项里选择第一个gcc就成,我也不清楚我的这两个选项为什么是一样的。选择了之后在.vscode文件夹内就会自动生成launch.json文件

Note:The program setting specifies the program you want to debug. Here it is set to the active file folder ${fileDirname} and active filename with the .exe extension ${fileBasenameNoExtension}.exe, which if helloworld.cpp is the active file will be helloworld.exe.

By default, the C++ extension won't add any breakpoints to your source code and the stopAtEntry value is set to false.

Change the stopAtEntry value to true to cause the debugger to stop on the main method when you start debugging.

Note: The preLaunchTask setting is used to specify task to be executed before launch. Make sure it is consistent with the task.json file label setting.

创建c_cpp_properties.json配置文件

如果你想自定义C/C++ extension的话,可以创建c_cpp_properties.json文件,比如控制C++版本,编译路径等等。
可以通过使用C/C++命令(Ctrl+Shift+P)来观察C++ configuration UI。

当你在这个界面中做出改动并且保存时,VSCode就会在.vscode文件夹内自动生成c_cpp_properties.json配置文件用来记录你所做的更改。

编译运行

F5即可

不懂的地方还是有挺多的,继续摸索。

原文地址:https://www.cnblogs.com/mirage-mc/p/14288957.html