CentOS7使用VS Code编译调试(Debug)C/C++项目

首先需要在CentOS7下要安装VSCode工具

对于怎么安装vscode,参考官方的https://code.visualstudio.com/docs/setup/linux 有详细的说明。

对于centos则如下:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'

然后用yum命令安装

yum check-update

sudo yum install code

安装以后在应用程序--》编程我们就能看到VSCode图标了,双击它就可以打开了

我们还需要安装cpptools,请自行完成。

点击左侧的Extensions扩展按钮

CentOS7使用VS Code编译调试(Debug)C/C++项目_第1张图片

然后在搜索栏里面输入c++

CentOS7使用VS Code编译调试(Debug)C/C++项目_第2张图片

然后点击安装

点击菜单 查看-> 调试,或直接快捷键ctrl + shift + D

 

点击设置图标,在弹出的选择环境中选择C++(GDB/LLDB),会自动创建一个launch.json文件(这里要注意一下,我们登陆的linux用户必须是root权限的,否则会弹出错误提示没有权限不能创建launch.json,所以我们要用root权限)

CentOS7使用VS Code编译调试(Debug)C/C++项目_第3张图片

我们用一个例子来做实验,来演示调试

在/opt下创建test文件夹,里面要放入我们要测试的项目文件

Makefile  solution.cpp  solution.h test.cpp

其中 solution.h为

/* solution.h */
class Solution {
  public:
    void Say();
};

 solution.cpp为

/* solution.cpp */
#include 
#include "solution.h"
void Solution::Say(){
   std::cout << "HI!" << std::endl;
}

test.cpp为

#include "solution.h"
int main () {
    Solution sln;
    sln.Say();
    return 0;
}

Makefile为

build: test.o solution.o

	g++ -o build test.o solution.o	#注意前面必须是tab,不能是空格

test.o : test.cpp solution.h

	g++ -g -c test.cpp

solution.o : solution.h solution.cpp

	g++ -g -c solution.cpp

clean :

	rm test.o solution.o build

这样在该目录下执行make后编译出来的就是名称为build的可执行文件,它开启了-g选项,所以是可以进行调试的

然后我们需要将其配置到launch.json文件中

CentOS7使用VS Code编译调试(Debug)C/C++项目_第4张图片

然后在test.cpp中相应的配置上断点

CentOS7使用VS Code编译调试(Debug)C/C++项目_第5张图片

点击Debug--》Start Debugging或者F5

CentOS7使用VS Code编译调试(Debug)C/C++项目_第6张图片

就可以进入断点调试了

CentOS7使用VS Code编译调试(Debug)C/C++项目_第7张图片

你可能感兴趣的:(Linux,CentOS7,CentOS,VSCode,C/C++,C++,Debug)