ubuntu18.04使用vscode调试C++代码

8 篇文章 0 订阅
4 篇文章 0 订阅

在这里插入图片描述
点击debug后如上图所示,提示could not find the task ‘g++ build active file’.(现在知道了是因为prelaunchtask没有找到label为这个的task)

尝试了一下该文所说方法
修改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++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/test",	//test是自己取的名字
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

结果:
在这里插入图片描述没有变化。提示是要configure task,应该是缺少tasks.json文件。
配置task:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${workspaceFolder}/ch02/test.cpp",
                "-o",
                "test"
            ]
        }
    ]
}

再debug,结果:
在这里插入图片描述还是不对。猜测是tasks.json和launch.json的preLaunchTask不一致所致。
全部改为test再试:
在这里插入图片描述提示launch的program 路径 does not exist。(估计此时的task已经运行了一次了,但到program处停下来了)
我再试试先执行task,ctrl+shift+b;

成功生成test文件
在这里插入图片描述但ch02中有个test,外面还有一个test,这个原因应该是命令中的问题。

在这里插入图片描述可见需要先配置 tasks文件,build task后才能debug。

补充:
但ch02中有个test,外面还有一个test,这个原因应该是命令中的问题。
问题是task配置的参数中只写了"test",改为下列参数后生成的test就只在ch02文件夹中了。

            "args": [
                "-g",
                "${workspaceFolder}/ch02/test.cpp",
                "-o",
                "./ch02/test"
            ],

在这里插入图片描述再debug检验一下之前debug使用的test是不是ch02中的那个test。
在这里插入图片描述debug成功,看来确实是使用了ch02中的test。但是为什么之前会在ch02外和ch02内同时生成test呢,既然没有指定路径,一般来说应当是在当前路径生成,那么就只应当在外面有一个test。
注意到.vscode文件夹是在C++算法文件夹下的,
猜测:莫非是vscode的某种自动检测造成了文件夹内也生成test?

我们来看看launch的配置中的与路径相关的地方:

"program": "${workspaceFolder}/ch02/test",
    "preLaunchTask": "test",

将preLaunchTask改为:

 "preLaunchTask": "./ch02/test",

再debug,
在这里插入图片描述注意到此时task的配置

"label": "test",

猜测是preLaunchTask与task的label不一致以至于debug时不能找到task。
官方文档说:
preLaunchTask——
to launch a task before the start of a debug session, set this attribute to the name of a task specified in tasks.json (in the workspace’s .vscode folder). Or, this can be set to ${defaultBuildTask} to use your default build task.

考虑到tasks.json和launch.json同在.vscode文件夹中,则launch.json通过preLaunchTask来启动task时只需要task的label即可。
可知label和preLaunchTask是必须相等的,而preLaunchTask是通过匹配label来寻找tasks.json中的某个task。
注意tasks.json文件中是可以配置多个task的,所以自然需要不同的label来区别不同task,这些task是在tasks.json文件中的,与build task后生成的test没有关系,而test是由launch.json中的program关联的。

再次将task改为:

            "args": [
                "-g",
                "${workspaceFolder}/ch02/test.cpp",
                "-o",
                "test"
            ],

build task结果发现只在ch02外生成了test,。。。。看来是第一次记错了?
在这里插入图片描述
此时再debug,
在这里插入图片描述
可见launch的program才是与build tasks后生成的test相关联的,此时由于program找不到ch02内的test,于是发生了错误。如果此时我将外面的test剪切到ch02内,
在这里插入图片描述
再debug,
在这里插入图片描述
发现居然在成功的同时,ch02外又生成了一个test,看来之前的并非我眼花了,而是在debug时program链接到了ch02中的test运行,而prelaunchtask又再一次运行了task而按照原来的task配置在ch02外面生成了一个test。
为了检验我的猜测,删除ch02外的test,修改task,
在这里插入图片描述

   "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${workspaceFolder}/ch02/test.cpp",
                "-o",
                "./ch01/test"	//该task build会在ch01中生成test
            ],

此时launch中

"program": "${workspaceFolder}/ch02/test",	//与ch02内的test相连

 "preLaunchTask": "test",	//与tasks中的label为test的task相连

在这里插入图片描述
debug成功且果然符合预期,猜测正确。那么既然build task的功能就是生成test,而我们已经先build task生成了test,然后再运行launch通过program与test相连来debug,launch中的prelaunchTask显得多余,我估计此时注释掉它是对debug没有影响的,试验一下:
在这里插入图片描述
试验成功!
那么prelaunchtask为什么会多余呢?我猜测debug其实并不需要先build task来生成test,而可以直接通过launch.json完成。(前提是tasks.json和launch.json均配置完毕)过程为先通过prelaunchtask启动task生成test,然后program链接test进行debug。试验一下:
先将test全删掉,配置task和launch,
在这里插入图片描述
task

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${workspaceFolder}/ch02/test.cpp",
                "-o",
                "./ch02/test"	//注意此处的.指的是workspace即C++算法文件夹
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch

{
    // 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++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/ch02/test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "test",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

开始debug,
在这里插入图片描述
试验成功!至此vscode的task与launch配置研究圆满结束!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Ubuntu 18.04是一款非常流行的Linux操作系统,也是一款非常适合开发者使用的操作系统。我们可以通过以下步骤在Ubuntu 18.04上基于VSCode搭建C语言开发环境。 第一步:安装VSCode 首先我们要从官方网站上下载VSCode的安装包。下载完成后,打开终端,进入到下载的安装包所在的目录,执行以下命令以安装: ``` sudo dpkg -i <package-name>.deb ``` 这里的`<package-name>`需要替换成你下载下来的文件名。然后我们执行以下命令以安装VSCode: ``` sudo apt-get install -f ``` 第二步:安装C/C++插件 安装VSCode后,我们需要安装C/C++插件。我们可以通过以下步骤来安装: 1.打开VSCode,点击左侧菜单栏的“拓展”按钮; 2.在搜索框输入“C/C++”并点击搜索; 3.找到“C/C++”插件并安装。 此时,我们就已经成功地安装了C/C++插件。 第三步:安装C语言编译器 在Ubuntu 18.04,我们可以使用`gcc`编译器来编译C语言代码。我们可以通过以下命令安装: ``` sudo apt-get install build-essential ``` 此时,我们就已经成功地安装了C语言编译器。 第四步:在VSCode编写和运行C代码 我们可以通过以下步骤在VSCode编写和运行C代码: 1.打开VSCode,创建一个新的`.c`文件; 2.编写C代码; 3.保存代码,并在保存时指定一个文件名和一个后缀名,如`hello.c`; 4.打开终端,进入到保存的文件所在的目录; 5.使用以下命令编译代码: ``` gcc -o hello hello.c ``` 6.执行编译后程序: ``` ./hello ``` 此时,我们就可以在终端看到程序的输出了。 总结: 在Ubuntu 18.04上基于VSCode搭建C语言开发环境主要需要安装VSCode、C/C++插件和C语言编译器,然后在VSCode编写和运行C代码。对于初学者来说,以上步骤就可以满足基本的开发需求了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值