当前位置:   article > 正文

VS Code配置C/C++环境_要为intallisense配置的编译器

要为intallisense配置的编译器

VS Code配置C/C++环境

1.下载Visual Studio Code

下载地址:https://code.visualstudio.com/

2. 下载MinGW

下载地址:https://sourceforge.net/projects/mingw-w64/files/

下载时勾选相关文件

  • mingw32-gcc.bin(c语言文件编译器)
  • mingw32-gcc-g++.bin(c++语言编译器)
  • mingw32-gdb.bin(调试编译后文件)

需要设置环境变量,使用下面的指令检查安装是否成功。

gcc -v
  • 1

3. VS Code设置

3.1 下载插件

在左侧工具栏的扩展中下载C/C++插件。

在这里插入图片描述

3.2 新建工作区

新创建一个文件夹,用于存放代码。然后用VS Code打开找个文件夹。

3.3 C++环境配置

c_cpp_properties.jsontasks.jsonlaunch.json这3个配置文件均可以手动创建,放在.vscode文件夹下。

3.3.1 配置编译器

快捷键Ctrl+Shift+P调出命令面板,输入C/C++,选择Edit Configurations(UI)进入配置。设置编辑器路径和IntelliSense模式。

在这里插入图片描述

编译器路径设置为D:/Download/MinGW/bin/g++.exe

在这里插入图片描述

IntelliSense模式设置为gcc-x64

在这里插入图片描述

配置完成后,此时在侧边栏可以发现多了一个.vscode文件夹,并且里面有一个c_cpp_properties.json文件,内容如下,说明上述配置成功。compilerPath改为自己安装的路径。

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "D:/Download/MinGW/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x86"
        }
    ],
    "version": 4
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.3.2 配置构建任务

快捷键Ctrl+Shift+P调出命令面板,输入tasks,选择Tasks:Configure Default Build Task进行配置。调用g++编译器基于源代码创建可执行文件。

在这里插入图片描述

然后再点击g++.exe build active file

在这里插入图片描述

此时.vscode文件夹中出现一个名为tasks.json的配置文件,这个文件也可以手动创建。commandcwd改为自己安装的路径。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "shell",
			"label": "g++.exe build active file",
            // g++的路径
			"command": "D:/Download/MinGW/bin/g++.exe",
			"args": [
				// 添加gdb调试选项
				"-g",
				"${file}",
				// 指定生成可执行文件的名称
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "D:/Download/MinGW/bin"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				// 表示快捷键Ctrl+Shift+B可以运行该任务
				"isDefault": true
			}
		}
	]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

3.3.3 配置调试设置

点击菜单栏的启动调试

在这里插入图片描述

选择C++(GDB/LLDB)

在这里插入图片描述

此时.vscode文件夹中出现一个名为launch.json的配置文件,修改文件内容。

如果报错Unable to create 'launch.json' file inside the '.vscode' folder (Cannot read property 'name' of undefined).的话,手动创建即可。miDebuggerPath改为自己安装的路径。

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            // 配置名称,将会在启动配置的下拉菜单中显示
            "name": "(gdb) Launch",
            // 调试前执行的任务,就是之前配置的tasks.json中的label字段
            "preLaunchTask": "g++.exe build active file",
            // 配置类型,只能为cppdbg
            "type": "cppdbg",
            // 请求配置类型,可以为launch(启动)或attach(附加)
            "request": "launch",
            // 调试程序的路径
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            // 程序调试时传递给程序的命令行参数,一般设为空即可
            "args": [],
            // 设为true时程序将暂停在程序入口处,一般设置为false 
            "stopAtEntry": false,
            // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
            "cwd": "${workspaceFolder}",
            "environment": [],
            // true显示外置的控制台窗口,false显示内置终端
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/Download/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

3.4 运行代码

路径不要有中文!!否则会报错。按F5运行程序。

#include<stdio.h>
// system头文件
#include <stdlib.h>
int main()
{
    printf("Hello C!\n");
    system("pause");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
'
运行

在这里插入图片描述

#include<iostream>
using namespace std;
int main()
{
    cout<<"Hello C++!"<<endl;
    system("pause");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

3.5 插件推荐

  • Code Runner:点击右上角小三角既可以运行程序。

在这里插入图片描述

  • Bracket Pair Colorizer:给()、[]、{}这些常用括号显示不同颜色,让层次结构一目了然。

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/954716
推荐阅读
相关标签
  

闽ICP备14008679号