文章目录
- 前言
- 一、原来的操作
- 二、使用vscode调试
- 1.编写task
- 1.cd build && cmake . .
- 2.make in docker
- 3. run in docker
- 4. all in one
- 5.x11 open&close
- 2.配置launch.json
- 总结
- 已知不足
- 补充
- 1.debug
- 2.顺序执行(下面的更好)
- 3.顺序执行
前言
提示:这里可以添加本文要记录的大概内容:
系统: ubuntu20.04
开发环境: cpp + vscode + cmake + docker
一、原来的操作
终端:
cd build
cmake ..
make -j14
#运行
调试基本上都是cout输出,然后编译运行,非常耗时不方便
二、使用vscode调试
1.编写task
标题栏->termianl(alt+T)->选择最后2项,进行修改,会生成.vscode文件夹(隐藏文件夹)和该文件夹下面的tasks.json,所有task相关的内容都由该文件配置
配置如下
1.cd build && cmake . .
{
"label": "cmake in docker",
"type": "shell",
"command": " nvidia-docker exec -it -w /build/ develop cmake ..",
"problemMatcher": []
}
解释:我使用的时nvidia-docker,这里根据你的配置来,后面的-it 和-w 可以查看docker --help,作用是将后面的 代码输入docker中的终端中。以上task完成了原来操作中的cd build 和cmake . .部分
2.make in docker
{
"label": "make in docker",
"type": "shell",
"command": "nvidia-docker exec -it -w /build/ develop make -j15",
"problemMatcher": "$gcc"
}
解释:同第一条类似,在docker终端执行make -j15
3. run in docker
{
"label": "run in docker",
"type": "shell",
"command": "nvidia-docker exec -w /build -e LD_LIBRARY_PATH=/build/lib:/3rdparty/lib -e QT_PLUGIN_PATH=/3rdparty/plugins develop /build/bin/demo",
"problemMatcher": []
}
解释:同上, 在docker终端中执行生成的可执行目标文件,-e 配置环境变量,具体可以查阅docker --help
4. all in one
{
"label": "build in docker",
"dependsOn": [
//"x11 open",
"cmake in docker",
"make in docker"
]
}
解释:此步,组合了cmake和make,按序执行cmake . .和 make
5.x11 open&close
{
"label": "x11 open",
"type": "shell",
"command": "xhost +local:docker > /dev/null "
},
{
"label": "x11 close",
"type": "shell",
"command": "xhost -local:docker > /dev/null "
}
解释:如果希望将docker中生成的图形界面输出,组合使用x11 open,类似4
2.配置launch.json
如下图,打开.vscode下launch.json文件
"configurations": [
{
"name": "Launch C++ Docker Debug",
"type": "cppdbg",
"request": "launch",
"program": "/build/bin/demo",
"args": [],
"stopAtEntry": false,
"cwd": "/build",
"environment": [
{
"name": "LICENSE_DIR",
"value": "/Licenses"
},
{
"name": "LD_LIBRARY_PATH",
"value": "/build/lib:/3rdparty/lib"
},
{
"name": "QT_PLUGIN_PATH",
"value": "/3rdparty/plugins"
}
],
"sourceFileMap": {
"/build": "/home/a/projects/cmake_projects/build"
},
"externalConsole": true,
"filterStderr": true,
"filterStdout": true,
"preLaunchTask": "build in docker",
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "nvidia-docker",
"pipeArgs": [
"exec",
"-i",
"-w",
"/build",
"--privileged",
"rvs-develop",
"sh",
"-c"
],
"debuggerPath": "/usr/bin/gdb"
},
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
解释:其中比较重要的是preLaunchTask,定义了编译的操作,在tasks.json中。
关于pipeTransport,由于我并未使用vscode的docker插件连接docker,如需编译,需要加上这一条。 如果你连接上了docker,可以进行修改
总结
配置launch.json启动你生成的软件,在preLaunchTask中调用tasks.json中的task编译生成。在文件中打上断点,即可做到一键(F5)先编译后调试cmake工程
已知不足
1. debug console只能输出std::cout, qDebug无法输出2. 更新CMakeLists.txt后cmake . .的task可能报错,多按一次F5即可
3. 最后启动调试时,软件启动会比较慢
补充
1.debug
如何将qDebug()的信息输出至内置的debug中:
1.在工程的main.cpp中添加下列函数:
void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
std::cout << msg.toLocal8Bit().data() << std::endl;
if (type == QtFatalMsg) abort();
}
2.在main函数体中添加:
qInstallMessageHandler(myMessageOutput);
注意添加头文件,这两步的目的主要是将qDebug的输出通道改为std::cout,qInfo等都适用
2.顺序执行(下面的更好)
因为配置文件中的task启动不依赖于上一步,所以cmake …和make是同时进行的,下面是修改后的task
{
"label": "cmake and make",
"type": "shell",
"command" : "docker exec -itw /build/ --env-file ~/projects/env.sh develop sh -c 'cmake -DCMAKE_BUILD_TYPE=Debug .. && make -j15'",
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
}
}
能保证每次先cmake … 成功后再执行make。 缺点是只修改一个文件时,编译比较慢
3.顺序执行
vscode tasks 项depends on 中有属性 dependsOrder, 如下,设为sequence,即为按序执行,默认为并行执行
{
"label": "build in docker",
"dependsOrder": "sequence",
"dependsOn": [
"x11 open",
"cmake in docker",
"make in docker",
]
},