最后更新于2021年6月1日 10:19:47
第一种方法
launch.json:
{
// 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": "launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tcpping",
"args": ["172.21.5.12"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "cmake build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json,关键在于这个-DCMAKE_BUILD_TYPE=Debug
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cmake build",
"command": "mkdir build; cd ./build ;cmake ../ -DCMAKE_BUILD_TYPE=Debug;make",
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
相当于代替了这里的build按钮,这个应该是cmake tools插件带的功能吧?
还有CMakeLists.txt在这种情况下不用修改,网上烂糟的拼一起就错了。
#项目名称
PROJECT(tcpping)
#定义变量
SET(SRC_LIST tcpping.c)
#打印用户的一些信息
MESSAGE(STATUS "This is BINARY dir " ${
tcpping_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir "${
tcpping_SOURCE_DIR})
#生成可执行文件
ADD_EXECUTABLE(tcpping ${
SRC_LIST})
#使用到了数学函数sqrt(),需要引入libm.so
TARGET_LINK_LIBRARIES(${
PROJECT_NAME} m)
#SET(CMAKE_BUILD_TYPE "${CMAKE_CXX_FLAGS} -g") 不要加这句不然断点根本停不下来
第二种方法
CMakeLists.txt中添加 SET(CMAKE_BUILD_TYPE “Debug”) 把第一种方法中那个 -DCMAKE什么什么删掉。