launch.json和tasks.json的配置来实现自动化调试,不用手动编译
launch.json内容
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件", // 配置名称,显示在启动配置下拉菜单中
"type": "cppdbg", //配置类型
"request": "launch", //请求配置类型
"program": "${workspaceFolder}/build/main", //代表的可执行文件的绝对路径;**注意使用时main或build/main改成自己对应路径make生成的可执行文件所在位置;**
"args": [], //传递给程序的命令行参数
"stopAtEntry": false, //可选参数。如果为true,则调试程序应在目标的入口点处停止。如果床底了processId,则不起任何作用
"cwd": "${workspaceFolder}", //cd到工程的顶层目录
"environment": [], //要添加到程序中的环境变量{"name":"config","value":"Debug"}
"externalConsole": false, //true:启动控制台;false:在vscode的集成的控制台显示
"MIMode": "gdb", //调试方式
//
"setupCommands": [
{
"description": "为 gdb 启用整齐打印", //
"text": "-enable-pretty-printing", //
"ignoreFailures": true //
}
],
"preLaunchTask": "Build", //调试前作的一个task,联合tasks.json进行自动化编译后调试
"miDebuggerPath": "/usr/bin/gdb" //调试程序的路径,要知道自己把调试程序安装到哪里了
}
]
}
注意上面这一行的内容适当修改
“program”: “${workspaceFolder}/build/main”, //代表的可执行文件的绝对路径;注意使用时main或build/main改成自己对应路径make生成的可执行文件所在位置;
tasks.json内容
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build" //进入build文件夹的绝对路径
},
//一个tasks中包含着三个小task
"tasks": [
{
"type": "shell", //定义任务是被认为是进程还是在shell中运行
"label": "cmake", //task名称
"command": "cmake", //task命令
//调用命令时传递的参数
"args": [
".."
]
},
{
"label": "make", //task名称
"group": {
"kind": "build",
"isDefault": true
},
"command": "make", //task命令
"args": [] //参数:无
},
{
"label": "Build", //task名称,和launch中preLaunchTask一样
"dependsOrder": "sequence", //按列出的顺序执行任务依赖项
//依赖项
"dependsOn": [
"cmake",
"make"
]
}
]
}
补充:CMakeLists.txt模板
"Print to console":{
"prefix": "CMake",
"body": [
"# 指定cmake最低版本",
"cmake_minimum_required(VERSION 3.0)",
"",
"# 项目名称:project(MAIN)",
"project(${1:PROJECT_NAME})",
"",
"# 有些变量可能和add_compile_options中的重复,要注意",
"# CMAKE_C_FLAGS:gcc编译选项",
"# CMAKE_CXX_FLAGS:g++编译选项;\"${CMAKE_CXX_FLAGS} -std=c++11\":设置编译标准为c++11",
"# CMAKE_BUILD_TYPE:编译类型;(Debug:调试;Release:发布)",
"# CMAKE_C_COMPILER:指定c编译器",
"# CMAKE_CXX_COMPILER:指定c++编译器",
"# EXECUTABLE_OUTPUT_PATH:可执行文件输出存放路径",
"# LIBRARY_OUTPUT_PATH:库文件输出存放的路径",
"# set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -std=c++11\")",
"# set(CMAKE_BUILD_TYPE Debug or Release)",
"# 添加编译选项:add_compile_options(-Wall -g)",
"# -g:输出带调试信息的可执行文件",
"# -O[n]:优化代码,一般为-O2",
"# -Wall:输出警告信息",
"# -std=c++11:设置编译标准",
"# -D:定义宏",
"# add_compile_options(-Wall -g)",
"",
"# 添加头文件搜索路径:include_directories(./include)",
"# include_directories(include_PATH)",
"",
"# 添加多个特定的库文件搜索路径:link_directories(LIB_PATH1 LIB_PATH2 ...)",
"# link_directories(LIB_PATH)",
"",
"# 相当前工程添加存放源文件的子目录,并可以指定中间二进制和目标二进制存放的位置:add_subdirectory(source_dir [binary_dir])",
"# add_subdirectory(source_dir)",
"",
"# 生成库文件:add_library(libname MODE source1 source2 ...)",
"# add_library(libname MODE source1 source2 ...)",
"# libname:生成库的名称",
"# MODE:库的模式:{SHARED:动态库; STATIC:静态库}",
"# source:源文件,可以使用set设置的变量代替",
"# add_library(libname MODE source)",
"",
"# 设置变量名称:set(VALUE_NAME ./src/test01.cpp ./src/test02.cpp);${}:获取变量的值",
"# 就是使用一个变量名称代替多个cpp文件,方便调用",
"# set(VALUE_NAME ./src/test01.cpp)",
"",
"# 发现一个目录下所有的源代码文件放置到一个列表中,并将该列表存储到一个变量中:aux_source_directory(PATH VALUE_NAME)",
"# PATH:源文件的路径",
"# VALUE_NAME:生成的变量名",
"# 如果main函数的源文件也在该路经中保存,那么直接可用add_executable(target ${VALUE_NAME})生成可执行的目标文件",
"# aux_source_directory(PATH VALUE_NAME)",
"",
"# 编译可执行文件:add_executable(target main.cpp ${VALUE_NAME});${}:获取变量的值",
"# add_executable(target main.cpp ${VALUE_NAME})",
"",
"# 为可执行文件链接共享库:target_link_lib(target library1 library2 ...)",
"# target_link_lib(target library1)",
"",
],
"description": "CMake framework"
}