VScode 运行调试c++程序

一、环境配置

  • 安装了vscode
  • 安装了 C/C++ 
  • 安装好了Mingw-w64工具,并设置好了环境变量
Mingw-w64是Windows下的GCC工具,检查是否安装好可以用以下命令:

g++ -v
gdb -v

二、创建工程

mkdir projects
cd projects
mkdir helloworld
cd helloworld
code

code . 命令在当前目录下的打开vscode,创建helloworld.cpp文件

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

三、编译 

1、tasks.json 文件

  • 主菜单栏选择 Terminal -> Configure Default Build Task.
  • 从下拉栏中选择 g++.exe build active file,来编译在编辑栏活跃的源文件。

  tasks.json 文件告诉vscode如何编译这个程序,调用 g++ 编译器将源文件编译成可执行文件。

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "task g++",
			"command": "D:\\path\\c++\\bin\\g++.exe",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": "build",
			"detail": "D:\\path\\c++\\bin\\g++.exe"
		},
		
	]
}

2、launch.json 文件 

  • 从主菜单,选择 Run -> Add Configuration ... 之后选择 C++(GDB/LLDB)
  • 从下拉栏中选择g++.exe build and debug active file.
  • program:调试入口文件的地址
  • cwd:程序启动调试的目录
  • miDebuggerPath:调试器的路径
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Windows Bash ",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": true,
      "pipeTransport": {
        "debuggerPath": "/usr/bin/gdb",
        "pipeProgram": "${env:windir}\\system32\\bash.exe",
        "pipeArgs": ["-c"],
        "pipeCwd": ""
      },
      "setupCommands": [
          {
              "description": "gdb description",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
          }
      ],
      "preLaunchTask": "task g++"
    },
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [
        ""
      ],
      "stopAtEntry": true,
      "cwd": "d:/code/projects/helloworld",
      "environment": [],
      "program": "${fileDirname}\\${fileBasenameNoExtension}",
      "internalConsoleOptions": "openOnSessionStart",
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "externalConsole": true,
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "task g++"
    }
  ]
}

注意:preLaunchTask 指定了在启动调试前应该执行的任务,因此应该和tasks.json文件中的label保持一致。 

四、开始调试

按 F5 ,或者Run > Start Debugging 。

五、参考

  1. VSCODE 运行调试c++程序 - 知乎 (zhihu.com)
  2. Get Started with C++ and Mingw-w64 in Visual Studio Code
  3. VsCode + gdb + gdbserver远程调试C++程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

**星光*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值