Windows平台 VSCode C/C++开发环境配置

一、 环境准备

  • vscode
  • 下载安装Mingw-w64,并在系统环境Path指向Mingw-w64的bin目录
    打开命令行,输入 :
g++ --version
gdb --version

可以看到输出结果

  • 安装VSCode Microsoft C/C++扩展
    在这里插入图片描述

二、创建并编译项目

1. 创建项目

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .

code .命令可以让vscode在当前目录打开项目 。

2. 创建源文件

新建 helloworld.cpp 输入下面代码后保存。


#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world" <<endl;
}

3. 简单编译一下

右键选择RunCode可以简单运行当前文件:
在这里插入图片描述

使用命令行编译:

g++ helloworld.cpp -o helloworld.exe

ctrl+shift+b可选择编译器。 这里选择g++

4. 创建tasks.json

创建 .vscode文件夹,并创建task.json文件:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
      "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
      "options": {
        "cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Ctrl+Shift+B ,选择G++编译:
在这里插入图片描述
当前目录下会生成helloword.exe。
可以按如下修改,编译所有CPP文件 并生成指定可执行的文件名:

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
        "args": ["-g", "${workspaceFolder}\\*.cpp", "-o", "${workspaceFolder}\\a.exe"],
        "options": {
          "cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

5. 单步调试

点击菜单: Run > Add Configuration… , 选择 C++(GDB/LLDB)
在这里插入图片描述

这时在.vscode下会自动创建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": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "指向自己的gdb.exe文件路径",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

回到源程序,设置一个断点:
在这里插入图片描述

按Ctrl+Shift+B编译程序,再按F5进入调试模式。

三、 C/C++配置

创建 c_cpp_properties.json文件来配置C/C++设置。
也可以在命令面板使用可视化配置:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xundh/article/details/105831147