再写ubuntu 下 VS code的C++配置——gcc和clang

阅读: 评论:0

再写ubuntu 下 VS code的C++配置——gcc和clang

再写ubuntu 下 VS code的C++配置——gcc和clang

喵哥在去年写了一篇关于VScode配置的文章,Ubuntu vscode的C++ tasks.json,当时只是记录了如何配置,但是没有说为什么这么配置,而且只有gcc的配置,而VScode一直把Clang放在最前面,并且Clang比gcc更加节省内存,运行更快。。。

在这里一并把gcc和Clang的配置记录下来,并且两者都会采用gdb和lldb的调试以作对比。

首先需安装相应的软件:

gcc、Clang、gdb、lldb、VScode。

sudo apt install gcc
sudo apt install gdb
sudo apt install clang
sudo apt install lldb

VScode安装可以去官网下载:/


注意:下面包含很多gif,由于CSDN文章的横向长度有限,比例严重失调,直接观看很影响您愉快的心情,建议点开图片观察微操作:)

VScode是基于文件夹管理代码的,所以可以先新建一个文件夹,如‘Main’,尽量不要用中文。

然后在VScode中打开刚刚新建的文件夹,并且在VScode中新建一个cpp到此文件夹:

比如,输入如下代码:

#include <bits/stdc++.h>
using namespace std;int main(){printf("Hello vscode!n");return 0;
}

现在暂时不可以在VScode中调试,需要在扩展中加入C/C++ IntelliSense, debugging, and code browsing。

然后就是重头戏了,配置launch.json和tasks.json。需要注意的是:下面这种方法不一定适用所有的时候,但是其得到的两个文件的配置说明可以作为参考。更加一般的情况可以继续向下面浏览,在2019年8月5号有更新一般的情况。

选择到debug,左上角DEBUG旁边是“No Configurations”,点击它,选择“Add Configurations”,选择“C++(GDB/LLDB)”,选择“clang++ build and debug active file ”,这样就生成了初始的launch.json和tasks.json文件。

//launch.json
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /?linkid=830387"version": "0.2.0","configurations": [{"name": "clang++ build and debug active file", // 配置名称,将会在启动配置的下拉菜单中显示 "type": "cppdbg", // 配置类型,这里只能为cppdbg"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)"program": "${fileDirname}/${fileBasenameNoExtension}", // 将要进行调试的程序的路径,在linux中文件的后缀名不是那么重要,这里可以不添加,但要与tasks.json的输出一致"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可"stopAtEntry": false,   // 设为true时程序将暂停在程序入口处"cwd": "${workspaceFolder}",  // 调试程序时的工作目录,在当前的工作路径即可,总之还是要与tasks.json的配置一致"environment": [],"externalConsole": true,    // 调试时是否显示控制台窗口,一般设置为true显示控制台"MIMode": "lldb",// 指定连接的调试器,可以为gdb或lldb。"setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "clang++ build active file",   // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的taskName相对应"miDebuggerPath": "/bin/lldb-mi" // 调试器路径。}]
}
//tasks.json
{"tasks": [{"type": "shell","label": "clang++ build active file",   // 任务名称,与launch.json的preLaunchTask相对应"command": "/bin/clang++",  //命令,与在终端编译的参数一样"args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}" //输出的可执行文件可以不要后缀名,为了方便:)],"options": {"cwd": "/bin"}}],"version": "2.0.0"
}

有时候clang、gcc、lldb、gdb的位置不一定跟上面的一致,可以用

whereis 关键字

来查找位置。


2019.08.05

其实后来喵哥配置其他文件夹的时候并没有这么顺利,不知道是不是VScode抽风。症状如下:

点击选择添加配置(Add configuration),然后选择C++(GDB/LLDB),他只会自动生成launch.json文件,而不会出现task.json。生成的launch.json为:

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "enter program name, for example ${workspaceFolder}/a.out","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}

这时,需要再快捷键ctrl+shift+p打开命令行,输入Tasks: Run task  -> Create tasks.json file from template  ->  Others Example to run an arbitrary external command.

默认生成的task.json为:

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

 可见上面生成的launch.json和task.json是不完整的,可以根据前面有备注的两段代码来自己配置.


现在就基本配置好了,gcc的配置也是一样的,只是在选择编译器的时候选择“g++ build and debug active file ”

//launch.json
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /?linkid=830387"version": "0.2.0","configurations": [{"name": "g++ build and debug active file","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": true,"MIMode": "gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "g++ build active file","miDebuggerPath": "/bin/gdb"}]
}
//tasks.json{"tasks": [{"type": "shell","label": "g++ build active file","command": "/bin/g++","args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"],"options": {"cwd": "/bin"}}],"version": "2.0.0"
}

可以发现,clang默认的是lldb调试,gcc默认的是gdb调试,其实clang和gcc都可以使用两种调试方式,只需要修改launch.json即可。

//gdb"MIMode": "gdb","miDebuggerPath": "/bin/gdb"//lldb"MIMode": "lldb","miDebuggerPath": "/bin/lldb-mi"

并且,clang和gcc的编译器选择只要在tasks.json修改即可

//gcc
"command": "/bin/g++",
或者
"command": "/bin/gcc",//clang
"command": "/bin/clang++",
或者
"command": "/bin/clang",

不知道是为什么,使用lldb调试无法在终端输出结果,应该与这个错误有关:

Loaded '[vdso]'. Cannot find or open the symbol file.

但是目前还没有找到解决方案,先挖个坑,等以后有时间再来填坑。

在终端可以用lldb手动调试是没有问题的。

具体表现如下(以gcc示例,clang也是一样的):

另外,需要强调的是:VScode是基于文件夹管理代码的,并且在执行某个代码的时候需要把主叶面切换到对应的文件,喵哥一开始经常会去执行launch.json,然后就会报错。。。基于这个原因,完全可以在一个文件夹下有多个含有main函数的文件,这与VS系列的IDE是不一样的。

比如,我在‘Main’文件夹中再新建一个文件test.cpp:

 

 

 

 

 

 

本文发布于:2024-01-28 09:45:22,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17064063276537.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:再写   ubuntu   code   gcc   clang
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23