visual studio code C++开发基础配置
1、下载安装
Visual Studio Code - Code Editing. Redefined
安装完成后打开vscode,点击红色圈出区域,在搜索框分别搜索“C/C++”以及“chinese”,安装C/C++插件(必须有)与简体中文插件
2、安装MinGW-w64
从清华大学镜像下载网速更快更稳定
msys2 | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source MirrorIndex of /msys2/distrib/x86_64/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
选最新版本
默认安装,在
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
配置系统变量
3、调试代码:
#include <iostream>
using namespace std;
class TaskQueue {
public:
// 删除拷贝构造函数和赋值操作符
TaskQueue(const TaskQueue& t) = delete;
TaskQueue& operator=(const TaskQueue& t) = delete;
// 公共的获取单例对象的函数
static TaskQueue* getInstance() {
if (m_taskQ == nullptr) {
m_taskQ = new TaskQueue;
}
return m_taskQ;
}
// 成员函数
void print() {
cout << "我是单例对象的一个成员函数..." << endl;
}
private:
// 私有构造函数
TaskQueue() = default;
// 单例对象指针
static TaskQueue* m_taskQ;
};
// 初始化静态成员变量
TaskQueue* TaskQueue::m_taskQ = nullptr;
int main() {
// 获取单例对象并调用其成员函数
TaskQueue::getInstance()->print();
return 0;
}
修改默认的task.json配置文件
{
"tasks": [
{
"type": "shell", // 新任务采用shell运行
"label": "C/C++: g++ 编译前清理", // 新任务名称为:C/C++: g++ 编译前清理
"command": "rm", // 执行rm命令,删除文件或目录
"args": [
"-rf", // 递归删除,忽略文件不存在的情况
"${fileDirname}/bin/${fileBasenameNoExtension}" // 待删除文件路径
],
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "D:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
参考资料:
VS Code 配置 C/C++ 编程运行环境(保姆级教程)_vscode配置c++环境-CSDN博客