Ubuntu下使用Visual Studio Code来编译和调试C++

最近想在Linux上编译C++代码,自己却一直习惯Windows上的IDE。以前公司要我写Linux代码的时候,我一般都是用eclipse + CDT,而eclipse这东西吧,我个人感觉因为加载组件太多了,打开非常慢,所以不怎么想用它。后来用了一下vim + cmake 来搞,编译是可以了,调试又有点麻烦。今天在逛CSDN的时候看了一篇帖子说visual studio code 可以编译调试C++,我马上跑去微软的网站找到了  c/c++ for vs code,我就简单记录下编译过程:

一、快捷键

1、Open the Command Palette (Ctrl+Shift+P)

2、You can now build your application with (Ctrl+Shift+B)

3、press(Ctrl+Shift+O),then enter the name of the symbol you're looking for

4、To search for a symbol in the current workspace,start by pressing (Crtl + T)

二、安装编译调试过程(我不重复写了)

三、调试第三方库,比如(muduo库)

由于VSCode是用GCC去编译的,熟悉GCC非常重要,如果不熟悉GCC可以参考《gcc技术参考大全》。

Ubuntu下使用Visual Studio Code来编译和调试C++

2.用vscod来打开 echo例子目录,截图如下:

Ubuntu下使用Visual Studio Code来编译和调试C++

2、配置launch.json,如下图:

Ubuntu下使用Visual Studio Code来编译和调试C++

首先点击 debug按钮(红色数字1),然后在点击 配置按钮(红色数字2),在弹出来的选项中选择:GDB。

最后 把"enter program name for example ${workspaceRoot}/a.out" 改成 "${workspaceRoot}/a.out"。(红色数字3和4)

3、配置tasks

 首先按快捷键: ctrl + shift +p ,再输入:>tasks,再弹出的下拉选项中选:Tasks:Configure Task Runner,如下图:

Ubuntu下使用Visual Studio Code来编译和调试C++

好了,我们把默认的tasks.json修改成如下图所示:

Ubuntu下使用Visual Studio Code来编译和调试C++

至于为什么要改成这样呢?可以用终端打开echo目录,输入:

g++ -g -o a.out echo.cc main.cc -I /home/lh/Downloads/build/debug-install/include/ -L /home/lh/Downloads/build/debug-install/lib/ -lmuduo_base -lmuduo_net -lpthread

就可以看到echo目录多了a.out文件。其实vscode也是用GCC命令来编译,它的参数(红色数字2)也基本和GCC的参数一样。

我们可以直接用快捷键:  ctrl + shift +B来编译,也可以看到echo目录下多出了a.out文件。

4、调试程序

首先设置断点(红色数字2)如下图:

Ubuntu下使用Visual Studio Code来编译和调试C++

点击 debug按钮(红色数字1),如下图

Ubuntu下使用Visual Studio Code来编译和调试C++

可以看到了各种local变量,证明调试成功了。

Visual Studio 的详细介绍:请点这里
Visual Studio 的下载地址:请点这里

相关推荐