美文网首页
Mac使用vscode调试c/c++

Mac使用vscode调试c/c++

作者: 不想当社畜 | 来源:发表于2019-03-18 15:18 被阅读0次

mac上在vsCode上进行c/c++程序的调试

目的在mac上使用vscode 和 lldg/gdb调试工具 对编写c/c++程序进行调试。

调试下面编写的c++代码,文件名称main.cpp,循环中的输出语句中设置断点进行调试。

#include<iostream>

int main(){

  int i = 0;

  for(i = 0;i<10;++i){
    
    std::cout<<i<<std::endl;

  }
  std::cout<<"done!\n";
  
  return 0;
}

二个需要的文件(一个负责编译 一个负责调试)

在使用vscode进行c++调试最重要的是需要launch.json文件(负责调试程序),但是经常也需要tasks.json文件(负责编译程序).
下面简单介绍一个对应文件如何编写和使用.

1. tasks.json

生成命令:(shift+command+p -> Tasks:Configure Tasks, -> Create tasks.json form templates -> Others )

主要是对之后写的代码进行编译,而该编译操作命令写成一个任务,使用vscode的task用来编译自己写的代码。

默认形成的任务(task),该任务的作用从shell上,执行echo hello 命令。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

但是默认的任务是不能使用(shift+command+B)执行的(会提示没有No build task to run ),没有build的任务去执行。 需要在该文件中添加以下内容:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

此时就可以使用(shift+command+B)执行默认任务了。

当想建立一个执行编译c++程序的任务基本模块表示为:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build main",
            "type": "shell",
            "command": "clang++",
            "args": [
                "main.cpp",
                "-o",
                a.out",
                "-g"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

参数说明:
"label": 任务的名称 (build main)
"type" : 任务的类型,一共有两种(shell/Process),其中shell表示先打开shell,再执行输入命令;process则直接执行命令 (由于编译c++ 需要借助shell上进行执行命令)
"command": 实际上执行的命令(c++ 使用clang++命令进行编译)
"args": 需要设置的一些参数,应该是跟再command命令后面的.(此处表示对main.cpp文件进行使用clang++编译,其中参数表示的命令: clang++ main.cpp -o a.out -g ; 当使用-g 表示c/c++调试必备的一些参数 同时会再文件目录生成一个可执行文件名开头,DSYM结尾的文件夹(a.out.DSYM)对应的文件夹)

详细参考:
VScode官方文档关于tasks.json的说明
Github上tasks.json的模板

2. launch.json

(在dubug上,找到设置,新建一个c++(GDB/LLDB))生成一个对应的文件

作用启动的命令,用来执行启动调试的。

默认生成的文件

{
    // 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": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

为了调试自己的任务只需修改paragram对应的参数即可。

{
    // 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": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb"
        }
    ]
}

参数说明

"name": 配置名称,之后会出现再调试窗口的启动配置上
"type": 配置类型(不知道是否可以修改TODO:)
"request": 请求配置类型,可以设置为 launch(启动) 或者 attach(附加)
"program": 进行调试的程序的位置(此处在当前文件夹下的a.out可执行文件)
"stopAtEntry": 设置为true时,程序将会暂停再程序的入口中
"cmd": 当前调试所在的路径
"externalConsole": 调试是否显示控制台窗口,true即显示控制台


详细说明:
Github中关于launch.json文件的说明

主要一定要在编译过程中添加-g命令,不然程序无法进行调试.


参考链接:
MAC+VS Code+C/C++调试配置

相关文章

网友评论

      本文标题:Mac使用vscode调试c/c++

      本文链接:https://www.haomeiwen.com/subject/htzvmqtx.html