# 环境
VSCode
macOS / Ubuntu
Codelldb
# 开始
- VSCode 安装插件
C/C++
CodeLLDB
C/C++ Clang Command Adapter
- 测试代码
main.cc
# include <iostream>
int main(int argc, char* argv[]){
std::cout << "hello vscode debug" << std::endl;
for(int i = 0 ; i < argc ; i++){
std::cout << argv[i] << std::endl;
}
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(vscode_debug)
set(CMAKE_BUILD_TYPE DEBUG)
add_executable(vscode_debug main.cc)
- 编译
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
-
配置调试文件(launch.json)
launch.json负责调试代码
task.json 负责编译代码
- 生成
launch.json
文件
-
修改配置文件
{ "version": "0.2.0", "configurations": [ { "type": "lldb", "request": "launch", "name": "Debug", "program": "${workspaceFolder}/build/vscode_debug", "args": [], // 参数 "cwd": "${workspaceFolder}", "preLaunchTask": "Build with Clang" // 与 task 保持一致 } ] }
- type: 配置类型(不知道是否可以修改TODO:)
- request: 请求配置类型,可以设置为 launch(启动) 或者 attach(附加)
- name: 配置名称,之后会出现再调试窗口的启动配置上
- program: 进行调试的程序的位置(此处在当前文件夹下的build/vscode_debug可执行文件)
- args: 参数 (./vscode_debug xxx yyy)
- cwd: 当前调试所在的路径
- preLaunchTask: 与task相关, 两边的值必须保持一致
- 调试
-
配置编译文件(task.json)
-
生成task.json文件
- Shift+Command+p
- Task: Configure Default Build Task
- 使用模板创建task.json文件
- Other -
修改配置文件
{ "version": "2.0.0", "tasks": [ { "label": "Build with Clang", //这个任务的名字在launch.json最后一项配置 "type": "shell", "command": "cd ${workspaceFolder}/build && cmake -DCMAKE_BUILD_TYPE=Debug .. && make -j4", "args": [], "options": { "cwd": "/usr/bin" }, "group": { "kind": "build", "isDefault": true } } ] }
- label: 任务的名称 (任务的名字在launch.json最后一项配置)
- type: 任务的类型,一共有两种(shell/Process),其中shell表示先打开shell,再执行输入命令;process则直接执行命令 (由于编译c++ 需要借助shell上进行执行命令)
- command: 需要执行的命令 (cmake … && make)
-
-
配置文件隐藏变量
${workspaceFolder} - the path of the folder opened in VS Code ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/) ${file} - the current opened file ${fileWorkspaceFolder} - the current opened file's workspace folder ${relativeFile} - the current opened file relative to workspaceFolder ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder ${fileBasename} - the current opened file's basename ${fileBasenameNoExtension} - the current opened file's basename with no file extension ${fileDirname} - the current opened file's dirname ${fileExtname} - the current opened file's extension ${cwd} - the task runner's current working directory upon the startup of VS Code ${lineNumber} - the current selected line number in the active file ${selectedText} - the current selected text in the active file ${execPath} - the path to the running VS Code executable ${defaultBuildTask} - the name of the default build task ${pathSeparator} - the character used by the operating system to separate components in file paths
Vim配置C++ Debug环境