【Windows版】VScode配置C++开发环境

一、参考资料

Visual Studio Code 配置C/C++编译及调试环境
Windows下VSCode配置C++环境
VSCode配置C++官方文档
Get Started with C++ on Linux in Visual Studio Code

二、步骤

1. 下载安装VSCode

VSCode下载

2. 安装c++插件

点击左侧栏第五个,在搜索框内输入【c++】,安装c++插件。
在这里插入图片描述

3. 生成 c_cpp_properties.json 文件

Ctrl+Shift+P调出命令面板
	==》输入C/C++,选择【Edit Configurations(UI)】进入配置
	==》在.vscode文件夹中自动生成 c_cpp_properties.json 文件

修改配置

  • 编译器路径D:/360Downloads/Software/MinGW/mingw64/bin/bin/g++.exe
  • IntelliSense 模式gcc-x64

4. 新建demo.cpp工程文件

#include<iostream>
using namespace std;
 
int main()
{
    cout << "hello world!" <<endl;
    return 0;
}

5. 生成 task.json 文件

task.json 文件,代码进行编译的配置文件。

选择 【Terminal】
	==》【configure task】
		==》【C/C++:g++.exe生成活动文件】

在这里插入图片描述
在这里插入图片描述

6. 编译cpp文件

shift+ctrl+B 执行编译,生成 demo.exe 文件。
在这里插入图片描述

7. 运行 demo.exe 可执行文件

ctrl+F5,运行 demo.exe

8. 生成 launch.json 文件

launch.json文件,代码进行调试(debug)的配置文件。

选择 【Run】
	==》【Add Configuration
		==》【C++(GDB/LLDB)==》【g++.exe - 生成和调试活动文件】

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

9. 调试 demo.exe

F5,调试 demo.exe
或者
Run ==》Start Debugging

三、注意事项

  • 生成 task.jsonlaunch.json 文件之前,务必切换到 demo.cpp 或者 demo.c文件,否则自动生成的 task.jsonlaunch.json 无法编译对应的 demo.cppdemo.c
  • 建议不同的编程语言采用不同的文件夹。
  • .vscode 文件夹备份一份,以后需要的时候直接复制即可,不用再花时间进行配置了。

四、相关配置

1. c_cpp_properties.json

{
    
    
    "configurations": [
        {
    
    
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:\\360Downloads\\Software\\opencv\\build\\x64\\MinGw\\install\\include", 
                "D:\\360Downloads\\Software\\opencv\\build\\x64\\MinGw\\install\\include\\opencv2"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/360Downloads/Software/MinGW/mingw64/bin/g++.exe",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

2. tasks.json

{
    
    
	"version": "2.0.0",
	"tasks": [
		{
    
    
			"type": "cppbuild",
			"label": "C/C++: g++.exe 生成活动文件",
			"command": "D:\\360Downloads\\Software\\MinGW\\mingw64\\bin\\g++.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe",

				"-I",  "D:\\360Downloads\\Software\\opencv\\build\\x64\\MinGw\\install\\include",
				"-I",  "D:\\360Downloads\\Software\\opencv\\build\\x64\\MinGw\\install\\include\\opencv2", 
        
				"-L", "D:\\360Downloads\\Software\\opencv\\build\\x64\\MinGw\\lib",         
				"-l", "libopencv_calib3d452",         
				"-l", "libopencv_core452",         
				"-l", "libopencv_dnn452",         
				"-l", "libopencv_features2d452",         
				"-l", "libopencv_flann452",         
				"-l", "libopencv_gapi452",         
				"-l", "libopencv_highgui452",         
				"-l", "libopencv_imgcodecs452",         
				"-l", "libopencv_imgproc452",         
				"-l", "libopencv_ml452",         
				"-l", "libopencv_objdetect452",         
				"-l", "libopencv_photo452",         
				"-l", "libopencv_stitching452",         
				"-l", "libopencv_video452",         
				"-l", "libopencv_videoio452"
			],
			"options": {
    
    
				"cwd": "${workspaceFolder}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "编译器: D:\\360Downloads\\Software\\MinGW\\mingw64\\bin\\g++.exe"
		}
	]
}

3. launch.json

{
    
    
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\360Downloads\\Software\\MinGW\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

猜你喜欢

转载自blog.csdn.net/m0_37605642/article/details/129940354