以上安装不做具体说明
进入此Extensions(Ctrl+Shift+X)菜单,install 以下item:C/C++, C++ Intellisense, CMake,CMake Tools
cmake编译代码,gdb调试程序主要在于三个文件的生成与配置,分别为tasks.json, c_cpp_properties.json, launch.json。
使用快捷键Ctrl+shift+p 弹出添加配置项。
以自己的项目为例:
我的项目目录:
CMakeLists.txt 内容为:
-
#CMake 要求的至少版本
-
cmake_minimum_required(VERSION
3.10)
-
-
#
set the project name
and version
-
project(Tutorial VERSION
1.0)
-
-
add_definitions(-std
=c
+
+
17)
-
-
#构建的输出类型,executable是app,library是动态库,最后的.cpp 是被构建的文件名
-
add_executable(Tutorial Tutorial.cpp)
-
#
add_library(Tutorial Tutorial.cpp)
choose Run > Add Configuration... and then choose C++ (GDB/LLDB).
配置如下:
-
{
-
/
/
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":
"(gdb) Launch",
-
"type":
"cppdbg",
-
"request":
"launch",
-
/
/
"program":
"${fileDirname}/${fileBasenameNoExtension}",
-
"program":
"${workspaceFolder}/build/Tutorial",
-
"args": [],
-
"stopAtEntry":
false,
-
"cwd":
"${workspaceFolder}",
-
"environment": [],
-
"externalConsole":
true,
-
"MIMode":
"gdb",
-
"setupCommands": [
-
{
-
"description":
"Enable pretty-printing for gdb",
-
"text":
"-enable-pretty-printing",
-
"ignoreFailures":
true
-
}
-
],
-
"preLaunchTask":
"Tutorial",
/
/与tasks.json 的label项目同名
-
"miDebuggerPath":
"/usr/bin/gdb"
-
}
-
]
-
}
添加tasks.json
并修改内容如下:
-
{
-
"version":
"2.0.0",
-
"tasks": [
-
{
-
"label":
"Tutorial",
/
/编译的项目名,build
-
"type":
"shell",
-
"command":
"cd ./build ;cmake .. ;make",
/
/编译命令
-
"group": {
-
"kind":
"build",
-
"isDefault":
true
-
}
-
},
-
{
-
"label":
"clean",
-
"type":
"shell",
-
"command":
"make clean",
-
-
-
}
-
]
-
}
添加c_cpp_properties.json文件
Ctrl+Shift+P,输入C/C++,选择C/C++: Edit Configurations(JSON)
内容如下:
-
{
-
"configurations": [
-
{
-
"name":
"Linux",
-
"includePath": [
-
"${workspaceFolder}/**"
-
],
-
"defines": [],
-
"compilerPath":
"/usr/bin/gcc",
-
"cStandard":
"gnu11",
-
"cppStandard":
"gnu++17",
-
"intelliSenseMode":
"gcc-x64",
-
"configurationProvider":
"ms-vscode.cmake-tools"
-
}
-
],
-
"version":
4
-
}
全部保存,配置完成。
开始编译Ctrl+Shift+B
编译输出:
打断电即可gdb调试:
从上面可以看到,会有堆栈信息,终端输出,变量监测和表达式查看。
4.可能遇到的问题
4.1 可以编译通过,但是在引用头文件时,有波浪号错误显示
解决方案:
方案1:
重新打开该工程目录即:关闭目录再打开
方案2:
在工作区会.vscode文件夹中,
打开命令行 输入
gcc -v -E -x c++ -
出现:
将此标出菜单内容添加到c_cpp_properties.json文件中,如下
-
{
-
"configurations": [
-
{
-
"name":
"Linux",
-
"includePath": [
-
"${workspaceFolder}/**",
-
"/usr/include/c++/7/**",
-
"/usr/include/x86_64-linux-gnu/c++/7/**",
-
"/usr/include/c++/7/backward/**",
-
"/usr/lib/gcc/x86_64-linux-gnu/7/include/**",
-
"/usr/local/include/**",
-
"/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/**",
-
"/usr/include/x86_64-linux-gnu/**",
-
"/usr/include/**"
-
-
],
-
"defines": [],
-
"compilerPath":
"/usr/bin/gcc",
-
"cStandard":
"gnu11",
-
"cppStandard":
"gnu++17",
-
"intelliSenseMode":
"linux-gcc-x64",
-
"configurationProvider":
"ms-vscode.cmake-tools"
-
}
-
],
-
"version":
4
-
}
重新打开文件目录就可恢复正常。