VSCode远程调试linux环境的C/C++(一)-CSDN博客
接着前一篇继续学习
在linux端安装相关编译环境
apt install gcc
apt install g++
apt install cmake
安装成功后开始写代码测试,先写个最简单的,文件结构如下
vscode里框架如下,刚开始没有lauch.json,点击最左侧的Run按钮可自动生成个,我自己添加了tasks.json
main.cpp代码如下
// main.cpp
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(void)
{
int sum = 10;
for (size_t i = 0; i < sum; i++)
{
cout << i << endl;
}
return 0;
}
launch.json代码如下,根据路径修改了 "program"
{
// 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++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/dev/test/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json代码如下,给出了linux下执行步骤,先cmake,再make
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/dev/test"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": [
]
},
{
"label": "Build",
"dependsOrder": "sequence", // 按列出的顺序执行任务依赖项
"dependsOn":[
"cmake",
"make"
]
}
]
}
CMakeLists.txt如下
cmake_minimum_required(VERSION 2.8.0) #声明使用的cmake最小版本(要小于我们linux安装的那个版本)
project(Test) #项目名称
add_compile_options(-Wall -std=c++11 -g) #增加编译选项 -g可调试
#include_directories(include) #声明头文件目录
add_executable(main main.cpp)
点击最左边的Run and Debug或者按F5即开始执行,可按F9加断点,左边可以看到停在断点时的堆栈,其它跟VS一样,可以按F10单步调试了
可以看到程序目录下生成的中间文件